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

2013/12/8

Making 1 Actor move another Actor

TotalNoob TotalNoob

2013/12/8

#
I'm making a game where I have to get 1 object to move another object. I want to do this by putting a isTouching statement somewhere and if it's touching and I press any key (pre-set key) that both the objects move. If I press the same key again it drops the other object. If possible I would like to know if anyone could show me an example with a LargeBall and SmallBall where the Largeball moves the Smallball by pressing any key. I'm new to Greenfoot so all help is welcome.
danpost danpost

2013/12/8

#
I will presume that the user controls the LargeBall object using pre-set keys and the SmallBall objects are stationary unless 'attached' to the LargeBall object. Correct me if I am wrong.
TotalNoob TotalNoob

2013/12/8

#
Yes, the LargeBall can be moved using the left-right-up-down key and the Smallball is stationary unless it's attached to the LargeBall. Once un-attached from the Largeball it becomes stationary again.
danpost danpost

2013/12/8

#
All you need then is an instance field in the LargeBall class that holds a SmallBall object:
1
private SmallBall smallBall = null;
Then use 'getKey' to determine when the pre-set key has been released. If the value of the 'smallBall' field is null when the key is released and it is intersecting a SmallBall object, then set 'smallBall' to that intersecting object. If the value of 'smallBall' is not null when the key is released, set the value of 'smallBall' back to null. Then, for any movement of the LargeBall object, if 'smallBall' is not null, copy the direction and distance moved for that SmallBall object.
TotalNoob TotalNoob

2013/12/8

#
The problems is I dont exactly know how to type that out, I get a bunch of errors once I try it. I'd like to ask if you could put up a code of it on how it should be done.
danpost danpost

2013/12/8

#
It would be better if you post what you have so far, so you could learn from it. Show the code, copy/paste the errors, explain exactly where the error occurs, etc.
TotalNoob TotalNoob

2013/12/8

#
I'm trying some things out i'll post what I have in a bit
Yogibaer Yogibaer

2013/12/9

#
May be the scenario "PushyMultiLevel" (scenario no 10101) does what you are looking for. Try and see the code.
You need to login to post a reply.