Create Sprite Within its Own Class

Create Sprite Within its Own Class
0.0 0


I am making a very basic game that requires you to shoot bullets out of a turret. At the moment the bullet sprite is created in my gamescene class and then moved to its destination where it just sits there (I haven’t done any memory management yet). I was thinking it would be great if I could move the bullet into its own class so that when it was done moving the bullet I could just call “delete this;”. My question is how do I create the bullet in its own class and have it show up in my Game scene. I am very new to cocos2d X and programming in general so simple is best.
Any and all help is greatly appreciated.


You make a bullet class that sub-classes CCSprite

class bullet : public cocos2d::CCSprite* {


then call your custom class (after you add everything you need) from where you do now in your GameScene


Thats what I thought however it is not doing anything at all. I have put lots of breakpoints and CCLOGs in to try and see what is happening but the whole function is completing perfectly.
here is the function in the bullet class

void Bullet::fireBullet(float destx, float desty){
    //loads the sprite sheet containing the sprite
    CCLog("sprite sheet loaded bullet");

    //gets the size of the screen
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    width = winSize.width;
    height = winSize.height;

    //finds the scale factor for x and y
    CCLog("scale factor found");

    //creates the bullet and draws it to the screen at x=50% and y = 10%
    bullet = CCSprite::createWithSpriteFrameName("bullet");
    bullet->setPosition(ccp(width * 0.5, height * 0.1));
    bullet->setAnchorPoint(ccp(0.5, 0.5));
    bulletBatch->addChild(bullet, 1);
    CCLOG("sprite drawn to screen in batch node");

    //gets the real move duration by finding the distance that the bullet
    //travels then dividing that by velocity which is 480 pixels per second
    length = sqrtf((destx * destx) + (desty * desty));
    velocity = 480/1;
    realMoveDuration = length/velocity;
    CCLog("realMoveDuration found");

    //tests to see if destx and desty values passed correctally and created a
    //move duration
    if (realMoveDuration == 0) {
        CCLOG("its equal to zero something is wrong");
        CCLOG("the real move duration is not zero therefor desx and desty have a value");

    //gets the destination as a ccpoint
    CCPoint destination = CCPoint((destx), (desty));

    //moves the sprite to the destination
   bullet->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, destination),NULL));
    CCLOG("should be fired bullet class");

    //this will kill the bullet it is throwing an error at the moment
    //i will fix the error
    CCLOG("the bullet is killed");


and here is how I call that function from my GameScene class

Bullet bo;
bo.fireBullet(destx, desty);

I am getting no errors at all the bullet object is being created and the function runs perfectly with no errors but it doesn’t draw or move the sprite.


Do you need to be adding your BulletBatch to anything?

are you adding anything to a CCLayer?


That is probably what I am missing but I dont know how to do that. I just did the same thing that I did to create sprites in the GameScene class. Would you be able to explain what I need to do or should I do some digging around? is it hard?


Well ,

CCSCene* have CCLayers*

on CCLayers* you add CCSprite**, CCNode**, CCLabel*, etc.

So make a new layer and add your sprites to it, add that layer to your scene.

Are you looking at the HelloWorld example?


Okay that makes sence. Ill check out some of the examples and work out how to make a new layer thanks for you help!


I want you to learn, so please ask questions.

Something like:

CCScene scene = CCScene::create;
layer = new CCLayer();

CCSprite* s = CCSprite::initWithFile(….);
layer.addChild(s, 1);



Does that go in my bullet class? And I assume that because I already have that in my game scene class I don’t create a new scene I just create the layer and add it to the existing game scene scene?


true no need to create another scene. I was just trying to be complete, yet without writing everything out.


Alright so I create a new layer in gamescene? Then I have to add my Bullet class which inherits from CCSprite to that layer. Then I call the function from my gamescene class to fire the bullet?


Looking at the helloworld example. Do I need to make my bullet class inherit from CCLayer. Then in the init function in bullet I would use something like

To add the CCLayer named bullet to the game scene?
Then my firebullet method would draw the sprite on the bullet layer which would be a layer in the GameScene class


it’s hard for me to comment on specifics like that. I am not the one writing the game. There are a large number of ways this could be done. Developers will vary.


I think you should read:


Okay cool ill start experimenting when I get home thanks for all your help its greatly appreciated!