-
dougwarner59Likes 0Problem Description
How do you animate a sprite from a sprite sheet? (the spritesheet name is “goblin.png” )
I set up a sprite sheet of a character running, dieing, and attacking originally for the monkey IDE (which I hated). Each sprite frame is 32x32 bits; there are 13 sprites on a horizontal row. the first 6 sprite frames are of the character running, the next 4 are of the character dieing and the last 3 frames are of the character fighting or digging.
-
Sonar Systems adminLikes 0
You could loop through each frame of the spritesheet that belongs to a certain animation and change the texture of a image after a certain amount of time.
-
dougwarner59Likes 0
I don’t know how to do it the way you are explaining or the actual way I want to do it. I need more of a coding explination than a verbal one. I went to the cocos site and found a way to animate a sprite from a spritesheet: http://www.cocos2d-x.org/wiki/Sprite_Sheet_Animation. but, when I load it up; I get errors. I assume the coding is meant for more experienced coders, because I had to declare Sprite1 by doing the following: cocos2d::Sprite *Sprite1; But it still gives errors on the following line of code: animFrames->addObject(frame); the error message is: (base operand of '→' has non-pointer type ‘cocos2d::Vector<cocos2d::SpriteFrame*>’) how can I resolve this problem? I copied and pasted my code below.
-----------------the code in the game Cpp file... it also show where the error is coming from in a comment next to the line of code making the error--------------------------------------
#include "GameScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene *Game::createScene(){
auto scene = Scene::create();
auto layer = Game::create();
scene->addChild(layer);
return scene;
}
bool Game::init(){
if(!Layer::init())return false;
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
mySprite = Sprite::create("ghost.png");//cocos2d::Sprite *mySprite;set up in the header file.
mySprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(mySprite);
auto sprAction = MoveTo::create(3,Vec2(100,100));
mySprite->runAction(sprAction);
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("thunder.wav");
//---------------sprite animation test START-------------------
SpriteBatchNode* spritebatch = SpriteBatchNode::create("gremlin.png");
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("gremlin.plist");
cocos2d::Sprite *Sprite1;//********* I had to add to remove an error.*************
Sprite1 = Sprite::createWithSpriteFrameName("gremlin_01.png");
spritebatch->addChild(Sprite1);
addChild(spritebatch);
Vector<SpriteFrame*> animFrames(13);
char str[100] = {0};
for(int i = 1; i < 13; i++)
{
sprintf(str, "gremlin_%02d.png", i);
SpriteFrame* frame = cache->getSpriteFrameByName( str );
animFrames->addObject(frame);//********************causes errors!!!****************************
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.3f);
Sprite1->runAction( RepeatForever::create( Animate::create(animation) ) );
//---------------sprite animation test END---------------------
return true;
}
Login to reply