This site requires JavaScript, please enable it in your browser!
Greenfoot back
Acituanbus
Acituanbus wrote ...

2018/2/2

Can anyone tell me what's wrong?

Acituanbus Acituanbus

2018/2/2

#
import greenfoot.*; public class Player extends Actor { int Speed = 1; private int location = 0; public void startlocation(int ort){this.location=ort;} public int getort(){return location;} public void actions(){ int direction = this.getRotation(); if (this.canmove()){ if (Greenfoot.isKeyDown("w")) {setRotation(-90); move(Speed);} if (Greenfoot.isKeyDown("a")) {setRotation(180); move(Speed);} if (Greenfoot.isKeyDown("s")) {setRotation(90); move(Speed);} if (Greenfoot.isKeyDown("d")) {setRotation(0); move(Speed);} } else{ if (Greenfoot.isKeyDown("w")) {setRotation(-90);} if (Greenfoot.isKeyDown("a")) {setRotation(180);} if (Greenfoot.isKeyDown("s")) {setRotation(90);} if (Greenfoot.isKeyDown("d")) {setRotation(0);} } } public void doors(){ Actor ondoor1 = getOneIntersectingObject(Door1.class); if (ondoor1!=null){ getWorld().removeObject(ondoor1); Greenfoot.delay(10); UnclesHouse uncleshouse = new UnclesHouse(); Greenfoot.setWorld(uncleshouse); Speed = 2; } public void act(){ actions(); doors(); } Can anyone tell me why "Speed=2" doesn't work? It just stays at 1, but it still switches to the new world (UnclesHosue) Any idea? }
Super_Hippo Super_Hippo

2018/2/2

#
The UncleHouse world probably creates a new Player object which has nothing to do with the one in the other world.
Acituanbus Acituanbus

2018/2/2

#
Super_Hippo wrote...
The UncleHouse world probably creates a new Player object which has nothing to do with the one in the other world.
How could I get the new player object to set Speed to 2 then?
Super_Hippo Super_Hippo

2018/2/2

#
Player player = new Player();
addObject(player, <x-coord>, <y-coord>);
player.Speed = 2;
Or create a setter method for the speed variable if you want to make speed private.
Yehuda Yehuda

2018/2/2

#
You can also have an int parameter in the new World's constructor method for the speed.
Nosson1459 Nosson1459

2018/2/2

#
create a constructor for player and add a parameter for speed so that wen u instantiate a new player your able to set it. put this in the player class and remove line 4
int speed;
public Player(int speed)
{
    this.speed=speed;
}
then wen u create a player object do this
addObject(new Player(/*the desired speed*/),/*the x and y cords*/);
danpost danpost

2018/2/2

#
Instead of:
Speed = 2;
try:
((Player)unclesHouse.getObjects(Player.class).get(0)).Speed = 2;
Acituanbus Acituanbus

2018/2/2

#
Thanks, I used the player.Speed=2 one, was the easiest one
You need to login to post a reply.