-
MoseHasLikes 0Problem Description
Hi there. I am now studying how to make my stars run animation. (Like slightly shaking or something like that)
I checked the Animation usage. They said that we need the picture and its plist file.
But I do not know how to modify it to make it display in animation style.
Here is my code.
#include "Star.h" #include "Definitions.h" Star* Star::create(int color) { Star* ret = new Star(); if (ret && ret->initWithFile(ret->getImage(color, 1))) { ret->color = color; ret->selected = false; ret->autorelease(); //ret->setOpacity(128); return ret; } CC_SAFE_DELETE(ret); return nullptr; } /* * To get the image path. * _switch: 1. get image.png | 2. get image.plist */ char* Star::getImage(const int& color,const int& _switch) { if (_switch == 1) { switch (color) { case enumColor::BLUE: return "blue.png"; case enumColor::GREEN: return "green.png"; case enumColor::YELLOW: return "orange.png"; case enumColor::RED: return "red.png"; case enumColor::PURPLE: return "purple.png"; } } else if (_switch == 2) { switch (color) { case enumColor::BLUE: return "blue.plist"; case enumColor::GREEN: return "green.plist"; case enumColor::YELLOW: return "orange.plist"; case enumColor::RED: return "red.plist"; case enumColor::PURPLE: return "purple.plist"; } } else log("star.cpp(Star::getImage) : error : 'Star::getImage' return value is invalid"); } void Star::updatePosition() { if(desPosition.y != getPositionY()) { /* * The falling speed of the stars *but still do not know why subtract 4 */ setPositionY(getPositionY() - MOVE_SPEED); if(getPositionY() < desPosition.y) setPositionY(desPosition.y); } if(desPosition.x != getPositionX()) { setPositionX(getPositionX() - MOVE_SPEED); if(getPositionX() < desPosition.x) setPositionX(desPosition.x); } }
And this is an example for animation creating I just found out on Google.
SpriteBatchNode* spritebatch = SpriteBatchNode::create("ball.png"); SpriteFrameCache* cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("ball.plist"); auto Sprite1 = Sprite::createWithSpriteFrameName("ball_0"); Sprite1->setPosition(visibleSize.width / 2, visibleSize.height * 0.4); spritebatch->addChild(Sprite1,100); addChild(spritebatch); Vector<SpriteFrame*> animFrames(10); char str[100] = { 0 }; for (int i = 0; i < 10; i++) { sprintf(str, "ball_%d", i); SpriteFrame* frame = cache->getSpriteFrameByName(str); animFrames.pushBack(frame); } Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.05f); Sprite1->runAction(RepeatForever::create(Animate::create(animation)));
How to bind them together? I have tried several ways and none of them worked.
Thank you.
-
Sonar Systems adminLikes 0
Did it come with a .plist
This reply has been verified.
-
MoseHasLikes 0
Yes, the animation should play the frame pictures by reading .plist.
This reply has been verified.
-
Sonar Systems adminLikes 0
Does he have a tutorial on how to use it?
This reply has been verified.
-
MoseHasLikes 0
The original source code did not contain animation part. I just want to make it cooler so I started studying putting every star animate.
Is that practicable?
This reply has been verified.
-
Sonar Systems adminLikes 0
Yes it is, first jsut learn animation without implementing it into your game.
This reply has been verified.
-
MoseHasLikes 0
SpriteBatchNode* spritebatch = SpriteBatchNode::create("ball.png"); SpriteFrameCache* cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("ball.plist"); auto Sprite1 = Sprite::createWithSpriteFrameName("ball_0"); Sprite1->setPosition(visibleSize.width / 2, visibleSize.height * 0.4); spritebatch->addChild(Sprite1,100); addChild(spritebatch); Vector animFrames(10); char str[100] = { 0 }; for (int i = 0; i < 10; i++) { sprintf(str, "ball_%d", i); SpriteFrame* frame = cache->getSpriteFrameByName(str); animFrames.pushBack(frame); } Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.05f); Sprite1->runAction(RepeatForever::create(Animate::create(animation)));
This is how I implement animation through .plist file, which I found the tutorial on Google.
However, my original way to create a sprite is creating a star class which is inherited from Sprite and initWithFile(), not createWithSpriteFrameName().
I have found that there is another way to create sprites through initWithSpriteFrameName(), but it says that I cannot use it due to the fact that it is non-static member function.
Well, now I have no idea how I could implement it.
-
Sonar Systems adminLikes 0
Coudl you show us a screenshot of the log.
-
MoseHasLikes 0
I tried to implement it and I realized that it was not impossible. Just I need to MODIFY the entire core function. So I give up.
(I have to change the return type to SpriteBatchNode to let the StarMatrix could get it, but the change would be too huge.)
Thank you.
-
Sonar Systems adminLikes 0
OK
Login to reply