Answer the question
In order to leave comments, you need to log in
How to implement control in the game?
Help to deal with the management of the hero. There is a scene, a hero is placed in it, he threw 3 buttons (CCNode) on the scene, to move left, right and jump. The control works, but when the hero passes further along the "map", then when you release and next press any button, there is no movement ... As if the frame remains at the beginning of the map, and the touch, as it were, already falls not on the button, but on the button sprite stand normally, they move with the hero. I work in xcode and spritekit. I present the code below:
-(void)update:(CCTime)delta{
float rad = _mainHero.rotation *(M_PI/180);
if (clickedRight) {
NSLog(@"нажата кнопка");
_mainHero.position = ccpAdd(_mainHero.position, ccp(delta *80, 0));
}else if (clickedLeft) {
_mainHero.position = ccpAdd(_mainHero.position, ccp(-1*delta *80, 0));
}
if (clickedJump) {
_mainHero.position = ccpAdd(_mainHero.position, ccp(sin(rad) * delta *150, cos(rad)*delta*150));
}
}
-(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
_follow = [CCActionFollow actionWithTarget:_mainHero worldBoundary:self.boundingBox];
[_contentNode runAction:_follow];
CGPoint touchLocation = [touch locationInNode:_contentNode];
if (CGRectContainsPoint([_btnRight boundingBox], touchLocation))
{
CCLOG(@"пробежалась по кнопке НАПРАВО !!!!---->>>");
}
self.position = ccp(0, 0);
if (CGRectContainsPoint([_btnRight boundingBox], touchLocation))
{
clickedRight = YES;
CCLOG(@"нажато на кнопку НАПРАВО !!!!---->>>");
clickedLeft = NO;
clickedJump = NO;
}
if (CGRectContainsPoint([_btnLeft boundingBox], touchLocation))
{
clickedLeft = YES;
clickedRight = NO;
clickedJump = NO;
CCLOG(@"нажато на кнопку НАЛЕВО !!!!---->>>");
}
if (CGRectContainsPoint([_btnJump boundingBox], touchLocation))
{
clickedJump = YES;
clickedLeft = NO;
clickedRight = NO;
CCLOG(@"нажато на кнопку ПРЫЖОК !!!!---->>>");
}
}
-(void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
clickedJump = NO;
clickedLeft = NO;
clickedRight = NO;
CCLOG(@"END---->>>");
}
- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event {
[self launchPenguin];
// ensure followed object is in visible are when starting
_follow = [CCActionFollow actionWithTarget:_mainHero worldBoundary:self.boundingBox];
[_contentNode runAction:_follow];
CGPoint touchLocation = [touch locationInNode:_contentNode];
}
-(id)init{
if (self = [super init]) {
self.multipleTouchEnabled = YES;
clickedJump = NO;
clickedLeft = NO;
clickedRight = NO;
self.position = ccp(0, 0);
_follow = [CCActionFollow actionWithTarget:_mainHero worldBoundary:self.boundingBox];
[_contentNode runAction:_follow];
}
return self;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question