是否可以在cocos2d中重新初始化对象

时间:2009-10-19 04:06:27

标签: cocos2d-iphone

我有一个名为sprite的昆虫子类。我创建了一个 GameLayer中该类的实例,然后初始化 它使用,

insect *bgg = [insect spriteWithFile:@"bird2a.gif"];

然后我设置一个计时器(10秒)来改变图像使用

*bgg = [insect spriteWithFile:@"2.gif"];

但我的程序崩溃。现在我的问题是可能的 重新初始化一个对象或它是不可变的?

我有另一个问题,当我使用

- (BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [touches anyObject];
CGPoint point2 = [touch locationInView:[touch view]];

CGPoint cpoint=[[Director sharedDirector] convertCoordinate:point2];
NSLog(@"In touch began");

CGPoint cpoint=[[Director sharedDirector] convertCoordinate:point2]; NSLog(@"In touch began");

在我的昆虫类中它无法检测触摸'bgg'对象声明 在GameLayer.But当我在GameLayer中使用此功能时它可以 检测触摸。

我的做法有什么不对?有人解释。

高级thanx回复。

1 个答案:

答案 0 :(得分:0)

您的程序崩溃是因为您有拼写错误。从'* bgg'中删除'*'。这意味着您正在取消引用指针,然后尝试将新对象的创建应用于取消引用的指针。你只想要

bgg = [insect spriteWithFile:@"2.gif"];

然而,仅仅为了改变图像而创建一个全新的精灵是太过分了。 SpriteTextureNode的子类,因此只需使用TextureNode的texture属性为Sprite提供不同的图像。

Texture2D *newImage = [[TextureMgr sharedTextureMgr] addImage:@"2.gif"];
bgg.texture = newImage;

相关问题