Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top