层更新不会在cocos2d-iphone中调用 [英] layer update does not get called in cocos2d-iphone

查看:112
本文介绍了层更新不会在cocos2d-iphone中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建立并运行我的iPad上的应用程序时没有调用HelloWorldLayer类'update:method'的问题。





以下是HelloWorldLayer的实现:

  //导入接口
#importHelloWorldLayer.h

//需要获取导航控制器
#import AppDelegate.h

const float MaxPlayerAccel = 400.0f;
const float MaxPlayerSpeed = 200.0f;

#pragma mark - HelloWorldLayer

// HelloWorldLayer实现
@implementation HelloWorldLayer
{
CGSize _winSize;
CCSprite * _playerSprite;
UIAccelerationValue _accelerometerX;
UIAccelerationValue _accelerometerY;

float _playerAccelX;
float _playerAccelY;
float _playerSpeedX;
float _playerSpeedY;
}

//帮助程序类方法创建一个场景,HelloWorldLayer作为唯一的孩子。
+(CCScene *)场景
{
//'scene'是一个自动释放对象。
CCScene * scene = [CCScene node];

//'layer'是一个autorelease对象。
HelloWorldLayer * layer = [HelloWorldLayer node];

//将图层作为子级添加到场景
[scene addChild:layer];

//返回场景
return scene;
}

//在init你需要初始化你的实例

- (id)init
{
if = [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)加速度计:(UIAccelerometer *)加速度计didAccelerate:(UIAcceleration *)加速度
{
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方法中计划更新:

  [self scheduleUpdate]; 

编辑:另外,碰巧看到代码的另一个可能的问题 - as image-hd.png 。在Cocos2D中,您只需要命名该文件,而不使用-hd前缀: image.png ,如果屏幕是retina则会自动选择HD版本。


I'm having an issue with the HelloWorldLayer class' update: method not being called when building and running the applications on my iPad.

Not really sure what the issue is as init: and accelerometer: didAccelerate: are called as expected.

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

解决方案

You need to schedule the update in your init method:

[self scheduleUpdate];

Edit: 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆