Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在cocos2d中创建敌人类_Iphone_Objective C_Cocos2d Iphone - Fatal编程技术网

Iphone 如何在cocos2d中创建敌人类

Iphone 如何在cocos2d中创建敌人类,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,我正在iphone上使用cocos2d 我想为敌人创建一个类 看到我的代码了吗 #import <Foundation/Foundation.h> #import "cocos2d.h" @interface Enemies : CCSprite { float imageHeight; } @property float imageHeight; +(id) enemies; @end @implementation Enemies @synthes

我正在iphone上使用cocos2d 我想为敌人创建一个类

看到我的代码了吗

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

 @interface Enemies : CCSprite {
      float imageHeight;
 }
 @property float imageHeight;
 +(id) enemies;
 @end


@implementation Enemies
@synthesize imageHeight;

+(id) enemies{ 
return [[[self alloc] initWithFile:@"enemy.png"] autorelease];
}
@end 
所以,每当我创建类的实例时,它都应该正确设置imageHeight

谢谢


注意:我正在努力改进ahmed,保持
敌人
方法不变,并将其添加到您的类中:

-(id) init { 
    if (self = [super init]) {
        float textureHeight = [enemy texture].contentSize.height;
        [self setImageHeight:textureHeight];
    }
    return self;
}

这将为
敌人
类的所有新实例设置
imageHeight

覆盖敌人类中的initWithFile。 然后在其内部调用[super initWithFile传递相同的文件名字符串]


之后,您可以在下一行中设置imageHeight变量,因为纹理将被加载,并且高度将可访问。

Cocos2d会根据图像本身自动设置某些东西的高度。查找sprite的setHeight属性,它应该很容易在文档中找到

但是,您的问题似乎需要重新编写。你的“敌人”应该是数据,与任何显示(精灵)无关,除非你打算制作一个真正的小应用程序

如果你可以使用C#并在iOS之间进行翻译,那么请查看XNA入门书籍,了解如何抽象超类敌人和玩家。它们的工作原理基本相同。(我希望我知道一本好的iPhone游戏编程书,但我打开的所有书都有缺陷或糟糕的设计,只有有经验的程序员才能通过


不管是谁,如果你混合了数据和图像,通常意味着重新设计你的代码

Enimies有属性imageHeight。图像高度是属性,所以你可以通过在不同的类中创建对象,然后创建object来直接设置imageHeight。imageHeight=你想要设置的值。我不明白你为什么创建+(id)enimiesbut如何设置imageHeight,我找不到类实例的处理程序!我是否必须这样写:imageHeight=[self-texture].contentSize.height;?。如果有更多的enimies对象,请尝试以下方法—(id)init{if((self=[super-init]){imageHeight=[super-texture].contentSize.height;返回self;}@pawan.mangal:非常感谢你,我觉得它就像一个符咒:)
-(id) init { 
    if (self = [super init]) {
        float textureHeight = [enemy texture].contentSize.height;
        [self setImageHeight:textureHeight];
    }
    return self;
}