I have a simple lunar lander game.
I compute positions and everything by integration – e.g. each turn I take vectors and combine them and then apply resulting vector to my lander.
Here comes the question, I have a button that I want to use for thrust.
How do I check if it is on during update method? I guess i will have some BOOL flag that gets set to YES when the button is pressed, but when do i set it to NO?
Some practical implementation would be great.
I use cocos2d-iphone and iOS.
Well, the pseudo code goes as follows:
We shall not use Buttons (aka CCMenuItem), since they provide callbacks only on touch up events. We want touch down, touch exit/entered, touch ended.
In your
CCScenethat you are displaying, either add a new child that is a subclass ofCCLayeror even use one of theCCLayers already present in theCCScene.In the init of your
CClayersubclass, setisTouchEnabledtoYES.- (void)ccTouchesBegan:... - (void)ccTouchesMoved:... - (void)ccTouchesEnded:... - (void)ccTouchesCancelled:...Finally, do your magic in these methods.
CGRectContainsPointwhether the touch is within the thrust area.