制作 CCSprite 的副本

发布于 2024-10-09 01:30:19 字数 159 浏览 0 评论 0原文

我正在寻找对我的 CCSprite 进行子类化的许多重复或克隆。这是我游戏中的敌人角色,我需要无数次复制它。我该怎么做?

有人告诉我应该创建一个 EnemyFactory 类,将敌人分组,并将它们存储起来以供游戏中的后续关卡使用。

如果有人能为我解释一下,我将不胜感激:)

I'm looking to make many duplicates, or clones, of my CCSprite that's been subclassed. It's an enemy character in my game, and I will need to duplicate it countless times. How can I do this?

I've been told I should make an EnemyFactory class that makes the enemies in groups, and stores them for later levels in the game.

If someone could please explain this for me, that would be greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-10-16 01:30:19

当然,听起来您想要一种机制来跟踪您正在创建的所有敌人,并观察/调整他们的位置?

我建议将新创建的敌人添加到 NSMutableArray 中,如下所示:

static NSMutableArray *allMyEnemies = [[NSMutableArray alloc] init];

int numberOfEnemies = 3;

for (int i = 0; i < numberOfEnemies; i++){
    EnemySpriteClass *enemy = [[EnemySpriteClass alloc] init];
    [allMyEnemies addObject:enemy];
    [self addChild:enemy];
}

然后,当您想要查看/调整敌人精灵位置时 - 例如在主游戏循环中,当它们攻击您的英雄时,请使用以下内容:

for (int i = 0; i < [allMyEnemies count]; i++) {
    EnemySpriteClass * obj = (EnemySpriteClass *)[allMyEnemies objectAtIndex:i];
    NSLog("Enemy sprite is at this position: x:%f y:%f", 
           obj.position.x, obj.position.y);
    //Then add logic to adjust that position if needed
    obj.position.x -= 50;       
}

查看官方 cocos2d 论坛进行此类精灵管理的一些好的便捷方法:
http://www.cocos2d-iphone.org/forum/topic/5971

Sure, it sounds like you want a mechanism to track all the enemies you are creating, and watch/adjust their locations?

I suggest adding newly created enemies to a NSMutableArray like so:

static NSMutableArray *allMyEnemies = [[NSMutableArray alloc] init];

int numberOfEnemies = 3;

for (int i = 0; i < numberOfEnemies; i++){
    EnemySpriteClass *enemy = [[EnemySpriteClass alloc] init];
    [allMyEnemies addObject:enemy];
    [self addChild:enemy];
}

Then when you want to look at/adjust the enemy sprite positions- say on the main game loop as they are attacking your hero, use the following:

for (int i = 0; i < [allMyEnemies count]; i++) {
    EnemySpriteClass * obj = (EnemySpriteClass *)[allMyEnemies objectAtIndex:i];
    NSLog("Enemy sprite is at this position: x:%f y:%f", 
           obj.position.x, obj.position.y);
    //Then add logic to adjust that position if needed
    obj.position.x -= 50;       
}

Check out the official cocos2d forums for some good convience methods to do this kind of sprite management:
http://www.cocos2d-iphone.org/forum/topic/5971

魂ガ小子 2024-10-16 01:30:19

这是我用来创造敌人的一个例子。

当您想要创建敌人时,在图层中添加以下内容:

Gamelayer.m

int numberOfEnemies = 3;

for (int i = 0; i < numberOfEnemies; i++){
    EnemySpriteClass *enemy = [[EnemySpriteClass alloc] init];
    enemy.position = ccp(50 + 50*i, 50);
    [self addChild:enemy];
}

然后基于 CCSprite 创建敌人类:

EnemySpriteClass.h

#import "cocos2d.h"

@interface EnemySpriteClass: CCSprite
{
}

-(id) init;

@end

EnemySpriteClass.m

#import "EnemySpriteClass.h"

@implementation EnemySpriteClass

-(id) init
{
    if( (self=[super init] )) {
        self = [CCSprite spriteWithFile:@"squid.png"];
        //Add AI, life other properties.
    }
    return self;
}

如果您在动态创建敌人时遇到性能问题,您可以随时批量创建它们然后当您希望它们出现在屏幕上时调用 [self addchild:enemy] 。

Here is an example of what I use to create enemies.

Within your layer add the following when you want enemies to be created:

Gamelayer.m

int numberOfEnemies = 3;

for (int i = 0; i < numberOfEnemies; i++){
    EnemySpriteClass *enemy = [[EnemySpriteClass alloc] init];
    enemy.position = ccp(50 + 50*i, 50);
    [self addChild:enemy];
}

And then create an enemey class based on CCSprite:

EnemySpriteClass.h

#import "cocos2d.h"

@interface EnemySpriteClass: CCSprite
{
}

-(id) init;

@end

EnemySpriteClass.m

#import "EnemySpriteClass.h"

@implementation EnemySpriteClass

-(id) init
{
    if( (self=[super init] )) {
        self = [CCSprite spriteWithFile:@"squid.png"];
        //Add AI, life other properties.
    }
    return self;
}

If your having performance issues creating them on the fly, you can always batch create them and then call [self addchild:enemy] when you want them on screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文