在 cocos2d-iphone 中没有调用层更新 [英] layer update does not get called in cocos2d-iphone
问题描述
我遇到了 HelloWorldLayer 类更新的问题:在我的 iPad 上构建和运行应用程序时未调用方法.
I'm having an issue with the HelloWorldLayer class' update: method not being called when building and running the applications on my iPad.
不太确定问题是什么,因为 init: 和 accelerometer: didAccelerate: 被按预期调用.
Not really sure what the issue is as init: and accelerometer: didAccelerate: are called as expected.
这里是HelloWorldLayer的实现:
Here is the implementation of HelloWorldLayer:
// Import the interfaces
#import "HelloWorldLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
const float MaxPlayerAccel = 400.0f;
const float MaxPlayerSpeed = 200.0f;
#pragma mark - HelloWorldLayer
// HelloWorldLayer implementation
@implementation HelloWorldLayer
{
CGSize _winSize;
CCSprite *_playerSprite;
UIAccelerationValue _accelerometerX;
UIAccelerationValue _accelerometerY;
float _playerAccelX;
float _playerAccelY;
float _playerSpeedX;
float _playerSpeedY;
}
// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
- (id)init
{
if (self = [super initWithColor:ccc4(94, 63, 107, 255)])
{
_winSize = [CCDirector sharedDirector].winSize;
_playerSprite = [CCSprite spriteWithFile:@"Player-hd.png"];
_playerSprite.position = ccp(_winSize.width - 50.0f, 50.0f);
[self addChild:_playerSprite];
self.accelerometerEnabled = YES;
NSLog(@"init: method executed");
}
return self;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
const double FilteringFactor = 0.75;
_accelerometerX = acceleration.x * FilteringFactor + _accelerometerX * (1.0 - FilteringFactor);
_accelerometerY = acceleration.y * FilteringFactor + _accelerometerY * (1.0 - FilteringFactor);
if (_accelerometerY > 0.05)
{
_playerAccelX = -MaxPlayerAccel;
}
else if (_accelerometerY < 0.05)
{
_playerAccelX = MaxPlayerAccel;
}
if (_playerAccelX < -0.05)
{
_playerAccelY = -MaxPlayerAccel;
}
else if (_playerAccelX > -0.05)
{
_playerAccelY = MaxPlayerAccel;
}
}
- (void)update:(ccTime)dt
{
NSLog(@"update: method is being called");
// 1
_playerSpeedX += _playerAccelX * dt;
_playerSpeedY += _playerAccelY * dt;
// 2
_playerSpeedX = fmaxf(fminf(_playerSpeedX, MaxPlayerSpeed), - MaxPlayerSpeed);
_playerSpeedY = fmaxf(fminf(_playerSpeedY, MaxPlayerSpeed), - MaxPlayerSpeed);
// 3
float newX = _playerSprite.position.x + _playerSpeedX * dt;
float newY = _playerSprite.position.y + _playerSpeedY * dt;
// 4
newX = MIN(_winSize.width, MAX(newX, 0));
newY = MIN(_winSize.height, MAX(newY, 0));
_playerSprite.position = ccp(newX, newY);
}
@end
推荐答案
您需要在 init 方法中安排更新:
You need to schedule the update in your init method:
[self scheduleUpdate];
另外,碰巧看到您的代码可能存在另一个问题 - 您正在将精灵图像加载为 image-hd.png
.在 Cocos2D 中,只需要命名不带 -hd 前缀的文件:image.png
,如果屏幕是retina,它会自动选择高清版本.
Also, happened to see another possible issue with your code - you're loading the sprite image as image-hd.png
. In Cocos2D, you only need to name the file without the -hd prefix: image.png
, and it will automatically select the HD version if the screen is retina.
这篇关于在 cocos2d-iphone 中没有调用层更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!