wie kann ich eine Variable die in einer Klasse erstellt wurde in einer anderen Klasse verwenden?
also auf das Snake Spiel bezogen wie kann ich die in "public class Snake_0 extends GameGrid" erstellte variable "startingSpeed" in "public void tryToEat(Location lastLocation)" von der Klasse "class Snake extends Actor " verwenden?
Hier der Code hab da mal ein paar Ausrufezeichen hin gemacht wo ich das meine ^^
Code: Alles auswählen
import ch.aplu.jgamegrid.*;
import java.awt.Color;
import java.util.ArrayList;
import java.awt.event.KeyEvent;
public class Snake_0 extends GameGrid
{
// geschwindigkeit
public int startingSpeed = 1000; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public Snake_0()
{
super(20 , 20 ,20, Color.darkGray, false);
reset();
show();
doRun();
}
public void reset()
{
removeAllActors();
// Nachricht oben einfügen
setTitle("Bewege die Schlange mit den Pfeiltasten.");
Snake snake = new Snake();
addActor(snake, new Location(10, 10));
addActor(new Mouse(), getRandomEmptyLocation());
snake.setDirection(Location.NORTH);
}
public static void main(String[] args)
{
new Snake_0();
}
}
// ______________ Snake ______________
class Snake extends Actor
{
public ArrayList<TailSegment> tailList = new ArrayList<TailSegment>();
public int tailSize = 0;
public Snake()
{
super(true, "sprites/snakeHead.gif");
}
public void act()
{
if (gameGrid.kbhit())
{
switch (gameGrid.getKeyCode())
{
case KeyEvent.VK_UP:
setDirection(270);
break;
case KeyEvent.VK_RIGHT:
setDirection(0);
break;
case KeyEvent.VK_LEFT:
setDirection(180);
break;
case KeyEvent.VK_DOWN:
setDirection(90);
break;
default:
return;
}
}
int lastIndex = tailList.size() - 1;
Location lastLocation = getLocation();
if (lastIndex > -1)
{
lastLocation = tailList.get(lastIndex).getLocation();
for (int i = lastIndex; i > 0; i --)
tailList.get(i).setLocation(tailList.get(i - 1).getLocation());
tailList.get(0).setLocation(getLocation());
}
move();
// wenn man das spielfeld verlässt
if (!isInGrid())
{
gameOver("Außerhalb des Spielfeldes!");
return;
}
// wenn man sich selbst isst
Actor a = gameGrid.getOneActorAt(getLocation(), TailSegment.class);
if (a != null)
{
gameOver("Du kannst dich nicht selbst essen!");
return;
}
tryToEat(lastLocation);
}
public void gameOver(String text)
{
gameGrid.setTitle(text);
gameGrid.removeAllActors();
gameGrid.addActor(new Actor("sprites/gameover.png"), new Location(10, 8));
gameGrid.doPause();
delay(5000);
gameGrid.doReset();
gameGrid.doRun();
}
public void tryToEat(Location lastLocation)
{
Actor actor = gameGrid.getOneActorAt(getLocation(), Mouse.class);
if (actor != null)
{
TailSegment newTail = new TailSegment();
gameGrid.addActor(newTail, lastLocation);
tailList.add(newTail);
tailSize++;
actor.removeSelf();
setSimulationPeriod(startingSpeed - 50); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
gameGrid.addActor(new Mouse(), gameGrid.getRandomEmptyLocation());
}
}
}
class Mouse extends Actor
{
public Mouse()
{
super("sprites/sMouse.gif");
}
}
// ____________ Snake Schwanz ________
class TailSegment extends Actor
{
public TailSegment()
{
super("sprites/snakeTail.gif");
}
}
Danke
Edit by cloidnerux: quote- zu code-Tags geändert