Ask About Animation Sprite (See My Code Please :) )

Ask About Animation Sprite (See My Code Please :) )
0

#1

Hi, I learn topic about animation sprite. I want my sprite have walk animation. then, when i pressed a button, the sprite change to attack animation.

Here is my code…

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

    b = 7;

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    auto bg = Sprite::create("BG.png");
    bg -> setPosition(Vec2(visibleSize.width/2 + origin.x , visibleSize.height/2 + origin.y));


mySprite = Sprite::create("male/Attack (1).png");
mySprite2 = Sprite::create("male/Attack (1).png");


mySprite -> setPosition(Vec2(visibleSize.width/2 - 150 + origin.x , visibleSize.height/2 + origin.y));
mySprite2 -> setPosition(Vec2(visibleSize.width/2 - 150 + origin.x , visibleSize.height/2 + origin.y));


mySprite -> setOpacity(0);
mySprite2 -> setOpacity(255);


Vector<SpriteFrame*> animFrames;
animFrames.reserve(8);
animFrames.pushBack(SpriteFrame::create("male/Attack (1).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (2).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (3).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (4).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (5).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (6).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (7).png", Rect(0,0,430,519)));
animFrames.pushBack(SpriteFrame::create("male/Attack (8).png", Rect(0,0,430,519)));

Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
animate = Animate::create(animation);
action1 = RepeatForever::create(animate);

Vector<SpriteFrame*> animFrames2;
animFrames2.reserve(10);
animFrames2.pushBack(SpriteFrame::create("male/Walk (1).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (2).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (3).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (4).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (5).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (6).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (7).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (8).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (9).png", Rect(0,0,430,519)));
animFrames2.pushBack(SpriteFrame::create("male/Walk (10).png", Rect(0,0,430,519)));

Animation* animation2 = Animation::createWithSpriteFrames(animFrames2, 0.1f);
animate2 = Animate::create(animation2);
action2 = RepeatForever::create(animate2);


mySprite->runAction(action1);
mySprite2->runAction(action2);

this->addChild(mySprite);
this-> addChild(mySprite2);

    auto but = MenuItemImage::create("HelloWorld.png","HelloWorld.png", CC_CALLBACK_1(HelloWorld::retryMenu, this));
    but -> setPosition(Vec2(visibleSize.width - 250 + origin.x , visibleSize.height/2 + origin.y));
    auto menu = Menu::create(but, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

schedule(CC_SCHEDULE_SELECTOR (HelloWorld::bantuan), 1.0f);


 
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    //Close the cocos2d-x game scene and quit the application

    
    /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
    
    //EventCustom customEndEvent("game_scene_close_event");
    //_eventDispatcher->dispatchEvent(&customEndEvent);
    
    
}

void HelloWorld::retryMenu(Ref* pSender)
{
    b = 0;
    mySprite2 -> setOpacity(0);
    mySprite -> setOpacity(255);
}

void HelloWorld::bantuan(float dt) 
{
    b++;
    if(b >= 2) {
    mySprite2 -> setOpacity(255);
    mySprite -> setOpacity(0);}
}

Yes, i can write the code. But, i think, that code is bad.

Can i just use 1 sprite with no variabel helper (int b) ? i have tried to just change the action in void HelloWorld::retryMenu , but when i pressed the button, the game crashes.


#2

Yes, you can use just one sprite and not 2. Probably the game crashes if you change the action in retryMenu because your actions are not retained, so after the init function finishes only the action that is currently running on a sprite is retained, the other gets destroyed(so you need to manually retain both actions once you have created them, and release them when you are sure you don’t need them anymore, maybe in a destructor).


#3

Ok, now i can animate my sprite :smile: . Then can i detect if my animation is finish ? So the sprite continue to use other animation (example : after i press the button, my sprite is atacking. After the animation finish, it return to walk animation) . I can use time schedule (example: if attack animation use 8 * 0.1 , so i delay the walk animation for 0.8 sec) . But , what happen if my game run in low budget / lag phone? That will be a problem


#5

Why are you bumping a 1.5 year old thread?


#6

dearest apologies, I didn’t see it as as old as the last ones I was notified for bumping


closed #7

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.