Sign in to follow this  

Java help please!

This topic is 2421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm getting a "cannot be resolved to type" error in Java. I tried looking it up to no avail.

There are three pieces of code in this sidecroller I am making here: [url="http://www.youtube.com/watch?v=abL3UVisK6g&annotation_id=annotation_348268&feature=iv"]http://www.youtube.c...8268&feature=iv[/url]

The problem is in the third .java, Board.java (look for !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)

[code]
package ss;

import javax.swing.JFrame;

public class Frame {

public Frame() {
JFrame frame = new JFrame("2D Game");
frame.add(new Board()); //Adds Board to Screen ala Frame
frame.setTitle("2D Test Game"); //title of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 365); //size of frame
frame.setVisible(true); //visiblity boolean
frame.setLocationRelativeTo(null);
}

public static void main(String[] args) {
new Frame();
}
}
[/code]

[code]
package ss;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Dude {

int x, dx; //Sprite Locations
int y, dy; // ""
int nx; //first frame variable
int nx2; //second frame variable
int left; //Will prevent the player from moving backwards. as per usual sidescroller games
Image still; // Stores Image
ImageIcon l = new ImageIcon("C:\\Users\\Devlin\\workspace\\My RPG\\src\\sprite left.png"); //Get Image
ImageIcon r = new ImageIcon("C:\\Users\\Devlin\\workspace\\My RPG\\src\\sprite right.png"); //Get Image
// ImageIcon j = new ImageIcon("C:\\Users\\Devlin\\workspace\\My RPG\\src\\sprite right.png"); //Get Image
// ^^ Jump Image

public Dude () {
still = l.getImage(); //left sprite
still = r.getImage(); //right sprite
left = 150;
x = 75; //controls x speed
int nx2 = 685; //Handles end of the FIRST frame (sidescrolling)
int nx = 0; //handles location of the second frame
y = 172; //controls y speed
}
public void move() {
if(dx != -1) { //Prevents player from moving backwards
if(left + dx <= 150)
left = left + dx;
else {
x = x + dx; //movement = current position + destination positio
//y = y + dy; //Controls up movement
nx2 = nx2 + dx; //End of frame (sidescrolling) a la character and scrolling frame should move at same speed
nx = nx + dx;
}
}else {
if(left + dx > 0) {
left = left + dx;
}
}

}
public int getX() { //This Method allows you to send x out of the class
return x;
}
public int getY() { //This Method allows you to send y out of the class
return y;
}
public Image getImage() { //Returns the image
return still;
}
public void keyPressed(KeyEvent e) { //Handles input
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) { //Controls movement left when key is pressed
dx = -1;
still = l.getImage();
}

if (key == KeyEvent.VK_RIGHT) { // ""
dx = 1;
still = r.getImage();
}
if (key == KeyEvent.VK_UP) {
dy = 1;
//still = j.getImage(); //For Jumping animation
}
//if (key == KeyEvent.VK_UP) {
// dy = -1;
//}
//
//if (key == KeyEvent.VK_DOWN) {
// dy = 1;
//}
}
public void keyReleased(KeyEvent e) { //Handles input
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) { //Controls left movement when key is released
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) { // ""
dx = 0;
}
if(key == KeyEvent.VK_UP) {
dy = 0;
// still = i.getImage() //Return from jumping image to normal image
}
//if (key == KeyEvent.VK_UP) {
// dy = 0;
//}
//
//if (key == KeyEvent.VK_DOWN) {
// dy = 0;
//}
//These methods are now sent to Board to be processed
}
}
[/code]
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Issue is with file below, it's complaining about the cycle method
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[code]
package ss;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener, Runnable { //JPanel Will be the window
Dude p; //Variable name of player class
Image img; //Stores image eventually
Timer time;
int v = 172; //used for animation, ground is at 172
Thread animator; //used for ... animation
boolean done = false; //Is jump done?
boolean h = false; //Has character reached maximum jump height?

public Board() { //Board Declaration
p = new Dude(); // p Will run player class
addKeyListener(new AL()); //keylistener == AL class below
setFocusable(true); //Will allow screen scrolling
ImageIcon i = new ImageIcon("C:\\Users\\Devlin\\workspace\\My RPG\\src\\redirect.png"); //Get Image
img = i.getImage(); //Image File == img variable
time = new Timer(5, this); //Will update screen every 5 milisecs
time.start(); //Start Time
}

public void actionPerformed(ActionEvent e) {
p.move(); //Move player
repaint(); //Repaint screen
}
boolean k = false;

public void paint(Graphics g) {
if(p.dy == 1 && k == false) {
k = true;
animator = new Thread(this); //init animator thread
animator.start(); //start animator thread
}
super.paint(g); //assign g to paint controller
Graphics2D g2d = (Graphics2D) g; //designate paint controller into 2D mode
if((p.getX() - 590) % 400 == 0)
p.nx = 0;
if(p.getX() == 1790)
p.nx2 = 0;
g2d.drawImage(img, 685-p.nx2, 0, null); //Second arg changed from 0 to p.nx2, which handles the speed at which the screen follows the player
if(p.getX() >= 590 ) {
g2d.drawImage(img, 685-p.nx, 0, null);
}
g2d.drawImage(p.getImage(), p.left, v, null); //Gets image fro Dude, uses get methods to set location
} // 75 Can be changed to p.getX() to get a adjustable value or p.left to prevent backwards scrolling
private class AL extends KeyAdapter { //Determines what key is released
public void keyReleased(KeyEvent e) { //Handled in Dude
p.keyReleased(e); //Passed from Dude
}
public void keyPressed(KeyEvent e) { //Handled in Dude
p.keyPressed(e);
}
}
public void run() {
long beforeTime, timeDiff, sleep; //All for time
//beforeTime is time before loop starts
//timeDiff is beforeTime - System.currentTimeMillis() aka how long it's been running
//sleep is for
beforeTime = System.currentTimeMillis();
while(done == false) {
cycle();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = 10 - timeDiff;
if (sleep < 0) { //Prevents negative sleep time
sleep = 2;
try {
Thread.sleep(sleep);
} catch(Exception e) {}
beforeTime = System.currentTimeMillis();
}
done = false;
h = false;
k = false;
}
public void cycle() { //Changes y locations
if(h == false)
v--; //Jump up
if(v == 125) //125 = max jump height
h = true;
if (h == true && v <= 172) //prevents player from falling through the floor
v++;
if(v == 172) //Is jump finished?
done = true;
}
}
}
[/code]

Your help is appreciated, when this is finished my char will be able to jump <3

Share this post


Link to post
Share on other sites

This topic is 2421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this