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

2023/1/4

placing object on another object

Mordanto Mordanto

2023/1/4

#
hi I have tried to place a rocket with the delay of 60 on the object test. However, I can no longer use the w,a,s,d control when I click on Run. I think the error is in the programming of the rocket. if (done1= false){ int x; int y; x=0; y=0; if (!getWorld().getObjects(Test.class).isEmpty()){ // if a Test object is in the world Actor actor = (Actor)getWorld().getObjects(Test.class).get(0); // get reference to Test object int TestX = x; // gets x of Test object int TestY = y; // gets y of Test object getWorld().addObject(new Rocket(),x,y); } done1=true; } Greenfoot.delay(60); done1=false;
danpost danpost

2023/1/5

#
Mordanto wrote...
hi I have tried to place a rocket with the delay of 60 on the object test. However, I can no longer use the w,a,s,d control when I click on Run. I think the error is in the programming of the rocket.
Actor actor = (Actor)getWorld().getObjects(Test.class).get(0); // get reference to Test object
Yes, you get a reference to the Test object and use actor as the name of the variable that holds that object. However, nowhere in the code following this line is this reference, actor, ever used.
danpost danpost

2023/1/5

#
int TestX = x; // gets x of Test object
int TestY = y; // gets y of Test object
These lines, as coded, do not get the Text object coordinates. They only take the values of the variables defined above it, which are both equal to zero, and set these new variables to those values. These might:
x = actor.getX();
y = actor.getY();
Mordanto Mordanto

2023/1/5

#
Thank you I have exchanged the two lines but I don't quite get what you meant in the first post and how to implement it.
danpost danpost

2023/1/5

#
Mordanto wrote...
Thank you I have exchanged the two lines but I don't quite get what you meant in the first post and how to implement it.
The second post shows how to use that reference to acquire the object's location coordinates.
You need to login to post a reply.