V
V
Vasily Bezfamilny2015-12-15 11:42:52
Objective-C
Vasily Bezfamilny, 2015-12-15 11:42:52

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;
}

the portal helps me in learning: https://www.makeschool.com/tutorials/getting-start... there is an implementation of the camera observing the "hero". But in my case, when the hero goes further than the starting "scene", the buttons remain behind, but the button icons go together, but they do not work. Please help) everything has stalled on this problem. I wanted to make the buttons move with the hero, that is, their position changed in the same way as the hero, but I think this will somehow be wrong, and that there is some good correct method.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question