I've got four simple methods, four buttons and one object.
- (IBAction)left:(id)sender{
object.center = CGPointMake(object.center.x - 5, object.center.y);
}
- (IBAction)right:(id)sender{
object.center = CGPointMake(object.center.x + 5, object.center.y);
}
- (IBAction)down:(id)sender{
object.center = CGPointMake(object.center.x, object.center.y + 5);
}
- (IBAction)up:(id)sender{
object.center = CGPointMake(object.center.x, object.center.y - 5);
}
When I press a button, the method is executed once. When the button is pressed continuously it's the same. What do I have to do so that when I press the button continuously my object keep moving to the left?
As @Maudicus said, you probably need to do something with
NSTimer
s to get a continuous button press triggering. The example I'm going to use is moving an object on screen (since you want to do that anyway). I've used graded moving because I have no idea whether you are writing a grid-based game and so need exactly 5 pixels of movement. Just cut out all of the stepSize code and set it to 5 if that's what you do.Write a timer callback function which checks whether a
BOOL
is set and if so keeps triggering itself:Create a timer property in the current classes
.h
:Synthesize it in your
.m
:Create your timer object when the button is pressed. Either do this in
touchesBegan:withEvent:
and check it's a Touch Down event, or connect the Touch Down event in Interface Builder to anIBAction
method.When the button is released (using one of the above methods again,
touchesEnded:withEvent:
for a Touch Up Inside or probably even a Touch Up Outside, or anotherIBAction
on both of those events), invalidate the timer externally:When a button gives its mousedown event, start moving. When the button gives its mouseup event, stop moving.
Be careful; this can act funny if multiple buttons can be pressed at the same time.
I think you need to schedule a timer and repeat a method that checks the button states.
// assume timer is set up to test button states, triggering
controlLoop
every x secondsI have never done this before so have fun playing around with it. I usually achieve controls like you want in Cocos2d,
It might be better to implement these methods
to set which direction you want to move the object, and still have a timer triggering the actual movement.