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

2015/2/15

difficulty faceing and turning a diraction

darkspawnwolf darkspawnwolf

2015/2/15

#
Its a Kinda weird Situation, I'm trying to set up a ship to be controlled key board. Everything seems to be working nicely except for the move up Key. I would Ideally Like to have the object/actor to rotate and face in the give direction like the other commands do however, when testing the first command on the line, it moves in the correct direction but doesn't face the direction. I also made a line of code were it as a "else" to have a pre_set facing location when not issuing commands durring run time. I would indefinitely like to know what i am doing wrong. Also note that I have attempted to (setLocation for 270 & -90) and both at the same time however the only results were a boost in a set speed..... lines of code below. { if (Greenfoot.isKeyDown("w")) { setRotation(270); move(1); } else { setRotation(0); move(0); } if (Greenfoot.isKeyDown("s")) { setRotation(90); move(1); } else { setRotation(0); move(0); }if (Greenfoot.isKeyDown("d")) { setRotation(0); move(1); }if (Greenfoot.isKeyDown("a")) { setRotation(180); move(1); } if ("space".equals(Greenfoot.getKey())) { fire(); } } /** * *fire the the Lemons */ private void fire() { Lemons_of_War lemons_of_war = new Lemons_of_War(); getWorld().addObject(lemons_of_war, getX(), getY()); lemons_of_war.setRotation(getRotation()); } }
danpost danpost

2015/2/15

#
If you follow the logic of the code, you will realize where the problem is coming from. The act method starts with this:
if (Greenfoot.isKeyDown("w"))
{
    setRotation(270);
    move(1);
}
else
{ 
    setRotation(0);
    move(0);
}
if (Greenfoot.isKeyDown("s"))
{
    setRotation(90);
    move(1);
}
else
{ 
    setRotation(0);
    move(0);
}
Now, assume the "w" key is pressed -- this is what happens: The first 'if' condition becomes true and the actor is rotated to 270 degrees and move up one cell. Then the second 'if' condition becomes false and the actor is then rotated to 0 degrees and does not move (btw, it will not move unless you tell it to; so, telling it to move zero is quite pointless). The net result is that the actor moves up one cell but ends up facing right.
You need to login to post a reply.