【问题标题】:-[_NSCFConstantString texture] error in Cocos2D- [_NSCFConstantString texture] Cocos2D 错误
【发布时间】:2013-11-05 07:43:38
【问题描述】:

我正在尝试为游戏创建精灵动画,当用户单击按钮时,它将产生 1 个单位/敌人,它们将在运行动画中在屏幕上移动。目前,当我运行游戏时,它会运行,一旦我尝试生成一个单位,游戏就会崩溃并给我一个 -[__NSCFConstantString texture]: unrecognized selector sent to instance 0x11a15c 错误。

这是单元本身的头文件:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "mainGameLayer.h"

@class MainGameLayer, Waypoint, Tower;

@interface Unit : CCSprite {
    CGPoint myPosition;
    int maxHP;
    int currentHP;
    float walkingSpeed;
    Waypoint *destinationWaypoint;
    BOOL active;
    float centerToBottom;
    float centerToSides;
}

@property (nonatomic, assign) MainGameLayer *theGame;
@property (nonatomic, assign) CCSprite *mySprite;
@property (nonatomic, strong) CCAction *walkAction;

+(id) nodeWithTheGame: (MainGameLayer *)_game;
-(id) initWithTheGame: (MainGameLayer *)_game;
-(void) doActivate;
-(void) getRemoved;

@end

下面是实现文件。 'self = super initWithSpriteFrame' 行当前正在抛出警告“不兼容的指针类型将 NSString * 发送到 'CCSpriteFrame *' 类型的参数。

此外,“CCSprite *frame = [[CCSpriterameCache.....”行抛出另一个警告说“标志 0 导致带有 p 转换说明符的未定义行为。

#import "Unit.h"
#import "Tower.h"
#import "Waypoint.h"

#define HEALTH_BAR_WIDTH 20
#define HEALTH_BAR_ORIGIN -10

@implementation Unit

@synthesize mySprite, theGame;

+(id) nodeWithTheGame:(MainGameLayer *)_game
{
    return [[self alloc] initWithTheGame:_game];
}

-(id) initWithTheGame:(MainGameLayer *)_game
{
    if ((self = [super initWithSpriteFrame:@"hero_walk_00.png"])) {
        theGame = _game;
        maxHP = 40;
        currentHP = maxHP;
        active = FALSE;
        walkingSpeed = 0.5;
        centerToBottom = 39.0;
        centerToSides = 29.0;

        CCArray *walkFrames = [CCArray arrayWithCapacity: 8];

        for (int i = 0; i < 8; i++) {
            CCSprite *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"hero_walk_%02.png", i]];
            [walkFrames addObject: frame];
        }

        CCAnimation *walkAnimation = [CCAnimation animationWithSpriteFrames:[walkFrames getNSArray] delay:1.0/12.0];
        self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnimation]];

        Waypoint *waypoint = (Waypoint *)[theGame.waypoints objectAtIndex:([theGame.waypoints count]-1)];
        destinationWaypoint = waypoint.nextWaypoint;

        CGPoint pos = waypoint.myPosition;
        myPosition = pos;

        [mySprite setPosition: pos];
        [theGame addChild: self];

        [self scheduleUpdate];
    }

    return self;
}

-(void) doActivate
{
    active = TRUE;
}

-(void)  update:(ccTime)dt
{
    if (!active) {
        return;
    }

    if ([theGame circle:myPosition withRadius:1 collisionWithCircle:destinationWaypoint.myPosition collisionCircleRadius:1]) {
        if (destinationWaypoint.nextWaypoint) {
            destinationWaypoint = destinationWaypoint.nextWaypoint;
        } else {
            [theGame getHpDamage];
            [self getRemoved];
        }
    }

    CGPoint targetPoint = destinationWaypoint.myPosition;
    float movementSpeed = walkingSpeed;

    CGPoint normalized = ccpNormalize(ccp(targetPoint.x - myPosition.x, targetPoint.y - myPosition.y));
    mySprite.rotation = CC_RADIANS_TO_DEGREES(atan2(normalized.y, -normalized.x));

    myPosition = ccp(myPosition.x + normalized.x * movementSpeed, myPosition.y + normalized.y * movementSpeed);

    [mySprite setPosition: myPosition];
}

-(void) getRemoved
{
    [self.parent removeChild:self cleanup:YES];
    [theGame.units removeObject: self];

    // Notify the game that we killed an enemy so we can check if we can send another wave
    [theGame enemyGotKilled];
}

-(void) draw
{
    ccDrawSolidRect(ccp(myPosition.x + HEALTH_BAR_ORIGIN, myPosition.y + 16), ccp(myPosition.x + HEALTH_BAR_ORIGIN + HEALTH_BAR_WIDTH, myPosition.y + 14), ccc4f(1.0, 0, 0, 1.0));

    ccDrawSolidRect(ccp(myPosition.x + HEALTH_BAR_ORIGIN, myPosition.y + 16), ccp(myPosition.x + HEALTH_BAR_ORIGIN + (float)(currentHP * HEALTH_BAR_WIDTH)/maxHP, myPosition.y + 14), ccc4f(0, 1.0, 0, 1.0));
}

@end

这里是游戏主层的头文件:

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface MainGameLayer : CCLayer {
    CCSpriteBatchNode *_actors;
}

+ (CCScene *) scene;
- (BOOL) circle:(CGPoint)circlePoint withRadius:(float)radius collisionWithCircle:(CGPoint)circlePointTwo collisionCircleRadius:(float)radiusTwo;
void ccFillPoly (CGPoint *poli, int points, BOOL closePolygon);
- (void) enemyGotKilled;
- (void) getHpDamage;

@property (nonatomic, strong) NSMutableArray *towers;
@property (nonatomic, strong) NSMutableArray *waypoints;
@property (nonatomic, strong) NSMutableArray *units;

@end

以及实现文件:

#import "MainGameLayer.h"
#import "Tower.h"
#import "Waypoint.h"
#import "Unit.h"


@implementation MainGameLayer

@synthesize towers;
@synthesize waypoints;
@synthesize units;

+ (CCScene *) scene
{
    CCScene *scene = [CCScene node];

    MainGameLayer *layer = [MainGameLayer node];

    [scene addChild: layer];

    return scene;
}

- (id) init
{
    if ((self = [super init])) {
        // Initialize
        self.isTouchEnabled = TRUE;
        CGSize winSize = [CCDirector sharedDirector].winSize;

        // Setting the background (Map)
        CCSprite *background = [CCSprite spriteWithFile:@"layout.png"];
        [self addChild: background];
        [background setPosition: ccp(winSize.width/2, winSize.height/2)];

        [self addWaypoints];

        // In Game Buttons / Menu
        CCMenuItem *sampleButton = [CCMenuItemImage itemWithNormalImage:@"sample.jpg" selectedImage:@"sample.jpg" target:self selector:@selector(samplePurchased:)];

        CCMenu *PurchaseUI = [CCMenu menuWithItems:sampleButton, nil];
        [PurchaseUI setScale:0.5];
        [PurchaseUI setPosition:ccp(63, 51)];
        [PurchaseUI alignItemsHorizontally];
        PurchaseUI.isTouchEnabled = TRUE;
        [self addChild: PurchaseUI];

        // Set up the sprite sheets (Currently in testing)
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"pd_sprites.plist"];
        _actors = [CCSpriteBatchNode batchNodeWithFile:@"pd_sprites.pvr.ccz"];
        [_actors.texture setAliasTexParameters];
        [self addChild: _actors];
    }

    return self;

}

-(BOOL) canBuyTower
{
    return YES;
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInView: [touch view]];

        location = [[CCDirector sharedDirector] convertToGL: location];
        CCLOG(@"X: %f Y: %f", location.x, location.y);

        if ([self canBuyTower]) {
            // Spend the gold later
            Tower *tower = [Tower nodeWithTheGame:self location: location];
            [towers addObject: tower];
        }
    }
}

-(void) addWaypoints
{
    waypoints = [[NSMutableArray alloc] init];

    Waypoint * waypoint1 = [Waypoint nodeWithTheGame:self location:ccp(-25,360)];
    [waypoints addObject:waypoint1];

    Waypoint * waypoint2 = [Waypoint nodeWithTheGame:self location:ccp(73,360)];
    [waypoints addObject:waypoint2];
    waypoint2.nextWaypoint =waypoint1;

    Waypoint * waypoint3 = [Waypoint nodeWithTheGame:self location:ccp(467,360)];
    [waypoints addObject:waypoint3];
    waypoint3.nextWaypoint =waypoint2;

    Waypoint * waypoint4 = [Waypoint nodeWithTheGame:self location:ccp(905,360)];
    [waypoints addObject:waypoint4];
    waypoint4.nextWaypoint =waypoint3;

    Waypoint * waypoint5 = [Waypoint nodeWithTheGame:self location:ccp(1050,360)];
    [waypoints addObject:waypoint5];
    waypoint5.nextWaypoint =waypoint4;
}

-(BOOL) circle:(CGPoint)circlePoint withRadius:(float)radius collisionWithCircle:(CGPoint)circlePointTwo collisionCircleRadius:(float)radiusTwo
{
    float xdif = circlePoint.x - circlePointTwo.x;
    float ydif = circlePoint.y - circlePointTwo.y;

    float distance = sqrt(xdif*xdif + ydif*ydif);

    if (distance <= radius + radiusTwo) {
        return TRUE;
    }

    return FALSE;
}

-(void) samplePurchased: (id)sender
{
    Unit *tempUnit = [Unit nodeWithTheGame: self];
    [units addObject: tempUnit];
    [tempUnit doActivate];
}

@end

我基本上只是在学习 Ray Wenderlich 网站上的一些教程;我之前没有遇到任何这些错误,也无法在 Google 上找到太多帮助。非常感谢这里的任何帮助!提前谢谢!

编辑:进行更改后,我现在可以看到一个不可见的精灵在屏幕上移动。精灵的健康条将出现并在屏幕上移动,但不会出现实际的精灵/动画本身。有什么原因吗?

【问题讨论】:

    标签: ios objective-c cocos2d-iphone


    【解决方案1】:
    [super initWithSpriteFrame:@"hero_walk_00.png"]
    

    期望 CCSpriteFrame* 作为参数,而不是 NSString*。这就是您收到此行的编译器警告的原因 - 不要忽略编译器警告!

    修复很简单,使用正确的初始化器:

    [super initWithSpriteFrameName:@"hero_walk_00.png"]
    

    这会返回一个 CCSpriteFrame* 而不是 CCSprite*:

    CCSprite *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] 
                 spriteFrameByName:[NSString stringWithFormat:@"hero_walk_%02.png", i]];
    

    因此是另一个警告。修复:

    CCSpriteFrame *frame = ...
    

    【讨论】:

    • 非常感谢!修复这两个编译器警告为我消除了错误!不幸的是,精灵/动画一旦生成就不会显示在屏幕上;我知道它在屏幕上,因为有一个健康栏在屏幕上移动,但精灵和动画本身没有出现。你有什么想法我可能搞砸了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多