iPhone - cocos2d - 动画和 C++ 类

iPhone - cocos2d - Animations and C++ class(iPhone - cocos2d - 动画和 C++ 类)

本文介绍了iPhone - cocos2d - 动画和 C++ 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将动画添加到我的游戏(iPhone 应用,使用 cocos2d).

I'm trying to add animations to my game (iPhone app, using cocos2d).

游戏是用 C++ 编写的,现在我想在 iPhone 上运行它,所以大部分类都是用 C++ 编写的.

The game was written in C++, and now I want to run it on iPhone, so most of the classes are in c++.

事情是这样的.我在 init 函数的 obj-c 类中创建 CCSprite,CCAction,然后在 sprite 上运行 CCAction.并且动画正在运行.

The thing looks like that. I'm creating CCSprite,CCAction in obj-c class in init function, and then run CCAction on sprite. And animation is working.

但是我想把这个 CCSprite 和 CCAction 变量放在我的 C++ 类中.我在初始化类中创建 *CCSprite 并将这个指针发送到 c++ 类.然后,我创建 CCAction 并在 sprite 上运行它.

But I want to put this CCSprite and CCAction variables in my C++ class. I create *CCSprite in init class and send this pointer to the c++ class. Then, I create CCAction and run it on the sprite.

然后,在我的 init 函数(obj-c 类)中:

And after that, when in my init function (obj-c class) do:

return self;

然后应用程序正在运行,运行和运行,没有任何反应.我在控制台中只收到这条消息:

then the app is running, running and running and nothing happens. I receive only this message in console:

* -[CCSprite setTexture:],/Users/Michal/..../libs/cocos2d/CCSprite.m:898 中的断言失败响应 SpringBoard 的终止而终止.

* Assertion failure in -[CCSprite setTexture:], /Users/Michal/..../libs/cocos2d/CCSprite.m:898 Terminating in response to SpringBoard's termination.

我不知道接下来该怎么做...是否可以将 CCSprite/Action 等成功保留在 C++ 类中?

I dont know what should I do next... Is it possible to keep CCSprite/Action etc. in C++ class succesfully?

推荐答案

在尝试像这样使用它之前,请确保已正确初始化纹理:

Make sure you have properly initialized the texture before you try to use it like this:

CCTexture2D *redButtonNormal = [[CCTextureCache sharedTextureCache] addImage:@"RedButtonNormal.png"];

然后初始化精灵(或精灵子类 - 在本例中 spuButton 是 CCSprite 的子类):

then initalize the sprite (or sprite subclass - in this example spuButton is a subclass of CCSprite) :

spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal];

注意:因为它是一个子类,所以我必须像这样为它设置 init 方法(如果你是 CCSprite 的子类,你必须这样做):

Note: since it is a subclass i had to setup the init methods for it like this (You must do this if you subclass CCSprite):

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture
{
    return [[[self alloc] initWithTexture:normalTexture] autorelease];
}

- (id)initWithTexture:(CCTexture2D *)aTexture
{
    if ((self = [super initWithTexture:aTexture]) ) {

        state = kButtonStateNotPressed;
    }

    return self;
}

这篇关于iPhone - cocos2d - 动画和 C++ 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iPhone - cocos2d - 动画和 C++ 类