![](/img/trans.png)
[英]Xcode [objective-c] cocos2d Unresponsive buttons in CCScrollLayer
[英]Objective-c [Xcode] CCAnimation not working, cocos2d
我在播放器類中聲明了兩個動畫,然后我想從另一個類中運行,但是我不能在這里輸入我的代碼(如果您在此處看不到它,則位於pastebin上: http : //pastebin.com/iy0eFMWL ) :player.h
@interface Player : CCSprite {
CCAnimate *animationOllie;
CCRepeatForever *repeatNormal;
}
player.m:
@implementation Player
-(id)initWithFile:(NSString *)filename
{
if (self = [super initWithFile:filename]) {
self.velocity = ccp(0.0, 0.0);
CCAnimation *ollie = [CCAnimation animation];
[ollie setDelayPerUnit:0.05f];
[ollie addSpriteFrameWithFilename:@"ollie1.png"];
[ollie addSpriteFrameWithFilename:@"ollie2.png"];
animationOllie = [CCAnimate actionWithAnimation:ollie];
CCAnimation *normal = [CCAnimation animation];
[normal setDelayPerUnit:0.05f];
[normal addSpriteFrameWithFilename:@"normal1.png"];
[normal addSpriteFrameWithFilename:@"normal2.png"];
CCAnimate *animationNormal = [CCAnimate actionWithAnimation:normal];
repeatNormal = [CCRepeatForever actionWithAction:animationNormal];
[self runAction:repeatNormal];
}
return self;
}
-(void)animateThePlayer {
[self stopAction:repeatNormal];
[self runAction:animationOllie];
}
在GameScene類中:GameScene.h:
@interface GamePlayLayer : CCLayerColor {
float yVel;
}
GameScene.m:
#import "Player.h"
@interface GamePlayLayer()
{
Player *player;
}
@end
@implementation GamePlayLayer
-(id) init
{
if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {
player = [[Player alloc] initWithFile:@"normal1.png"];
[self addChild:player];
self.isTouchEnabled = YES;
player.position = ccp(85,70);
[self schedule:@selector(update:)];
}
return self;
}
-(void)update:(ccTime)dt {
if (player.position.y > 70) {
yVel -= 0.1;
}
else {
if (yVel != 5.5) {
yVel = 0;
player.position = ccp(player.position.x, 70);
}
}
player.position = ccp(player.position.x, player.position.y + yVel);
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
yVel = 5.5;
[player animateThePlayer];
}
就是這樣,它構建良好,並且一切正常,但是當我單擊該圖層時它崩潰了,我得到以下消息:
0x1df609b:movl8(%edx),%edi線程1:EXC_BAD_ACCESS(code = 2,address = 0xf
我能做什么? 提前致謝
第一件事-您嘗試使用自動釋放的對象。 animationOllie
將在您的init方法之后釋放。
第二個錯誤是您不能重用動作。 當需要執行操作時,必須重新創建它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.