Applying a moveto action on a sprite causes the app to crash

  • warning717
    Likes 0

    Problem Description

    Im bringing a sprite in from cocos studio but the moveto action causes the scene to crash. Am I implementing the header incorrectly?

    CPP

    Sprite *custom = ( Sprite * )rootNode->getChildByTag( 41 );
    MoveTo *moveAction = MoveTo::create( 2, Vec2( 50, 100 ) );
    custom->runAction( moveAction );


    H

    cocos2d::Sprite * custom

  • Sonar Systems admin
    Likes 1

    Header is fine but in the CPP you are declaring a local copy, so change 

    Sprite *custom = ( Sprite * )rootNode->getChildByTag( 41 );

    to

    custom = ( Sprite * )rootNode->getChildByTag( 41 );

     

     

  • warning717
    Likes 0

    It still crashes :/

  • warning717
    Likes 0

    No solution? :(

  • Sonar Systems admin
    Likes 0

    Could you show us the entire .h and .cpp code

  • warning717
    Likes 1

    Something important, which I apologize for not mentioning, is that the sprite was inside a panel. However I have solved it by doing the following:

    CPP
    
    init
    custom = ( Sprite * )panel3->getChildByTag( 41 );
    }
    
    void MainMenu::touchEvent( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        MoveTo *moveAction = MoveTo::create( 2, Vec2( 50, 100 ) );
    
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            panel3->setVisible( true );
            custom->runAction( moveAction );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }
    
    H
    cocos2d::ui::Layout * panel3;
    cocos2d::Sprite * custom;

    Placing “MoveTo *moveAction = MoveTo::create( 2, Vec2( 50, 100 ) );” before the touch events and stating that the sprite is inside of the panel.
    Thanks for your help :)

  • Sonar Systems admin
    Likes 0

    GREAT TO HEAR :D

Login to reply