-1

Hello guys I am new to GreenFoot Java, however with ActionScript 3 I'm okay.

Now I hear that AS3 and Java are very similar.

In Green Foot application can I use

public void act() 
{
    if (e.keycode == 39)
       {this.x +=4};
}   

Or does this only apply for ActionScript 3? Sorry if the experts find this question stupid.

psxls
  • 6,807
  • 6
  • 30
  • 50
Moynzy
  • 28
  • 6

1 Answers1

0

Nope, this won't work. I presume there you're trying to find the keycode of an event, and while a similar model exists in native Java, Greenfoot uses a simpler mechanism.

I also assume by adding 4 to this.x you aim to move the component horizontally? Again similar concepts exist, but not in that syntax exactly.

In terms of replicating the above, it would be:

if(Greenfoot.getKey().equals("t")) { //Obviously replace t with whatever key you want
    setLocation(getX()+4, getY());
}

You should read the Greenfoot API documentation which shows you what methods you have available - if you simply try and crowbar Actionscript syntax into any Java application then fight the compiler until it works, you'll come more than a bit unstuck! While Actionscript and Java aren't too far apart, they're certainly not so similar you can work in that way.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Hey Berry120, thank you for the answer. I've made a green foot application but I need some help with something. Do you know any tutorials on kicking a ball. //If baby is touching ball (from this point (0.-10)&& e.keycode 32 (space bar) is pressed { //this.y += 5; } Here's my pseodu code. – Moynzy Dec 29 '13 at 18:05
  • @Moynzy Not for kicking a ball specifically, but the key topic you'll need for that is collision checking. Have a look at this tutorial (http://www.greenfoot.org/doc/tut-3) which covers that - the other tutorials would be worth working through also, if you haven't already. – Michael Berry Dec 29 '13 at 18:07
  • Thank you very much Berry, great help. – Moynzy Dec 29 '13 at 18:09