多个相同节点但没有连接 [英] multiples of the same nodes but have them not connected

查看:65
本文介绍了多个相同节点但没有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有两种颜色的圆圈(红色和绿色)从屏幕的右侧移动到左侧.现在正在使用此函数调用这些圆圈或节点

Currently i have two colors cirlces (red and green) moving from the right side of my screen to the left. Now these circles or nodes are being called using this function

-(void)move {
if(_dead)
    return;
int random = rand() % 2;

NSLog(@"int:%d",random);

if (random == 0) {
_enemy = [SKSpriteNode spriteNodeWithImageNamed:@"greeny"];


       _enemy.position = CGPointMake(CGRectGetMidX(self.frame)+200,
                                      CGRectGetMidY(self.frame));
    _enemy.size = CGSizeMake(70, 70);
    _enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
    _enemy.physicsBody.mass = 0;
    _enemy.physicsBody.categoryBitMask = Collisiongreen;


        [_enemies addObject:_enemy];
        NSLog(@"Enemies%lu",(unsigned long)_enemies.count);
          [self addChild:_enemy];
[_enemy runAction:[SKAction moveByX:-900 y:0 duration:4]];
    [self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
    [self runAction:[SKAction sequence:@[
                                         [SKAction waitForDuration:1.4],
                                         [SKAction performSelector:@selector(move) onTarget:self],

                                       ]]];
        NSLog(@"Enemies%lu",(unsigned long)_enemies.count);

    }
    if (random == 1) {
        _enemyy = [SKSpriteNode spriteNodeWithImageNamed:@"redy"];
        _enemyy.position = CGPointMake(CGRectGetMidX(self.frame)+200,
                                       CGRectGetMidY(self.frame));
        _enemyy.size = CGSizeMake(70, 70);
        _enemyy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
        _enemyy.physicsBody.mass = 0;
        _enemyy.physicsBody.categoryBitMask = Collisionred;


        [_enemies addObject:_enemyy];
        NSLog(@"Enemies%lu",(unsigned long)_enemies.count);
        [self addChild:_enemyy];
        [_enemyy runAction:[SKAction moveByX:-900 y:0 duration:4]];
        [self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
        [self runAction:[SKAction sequence:@[
                                             [SKAction waitForDuration:1.4],
                                             [SKAction performSelector:@selector(move) onTarget:self],



                                         ]]];
    NSLog(@"Enemies%lu",(unsigned long)_enemies.count);

}

感谢 Roecrew (touchesMoved 在特定的移动物体上)当我触摸绿色圆圈时,它会随着我的手指移动.

I have it set up thanks to Roecrew (touchesMoved on a specific moving object) That when I touch the green circle it will move with my finger.

我的问题:每当在绿色节点之后创建绿色节点或在红色节点之后创建红色节点时,先前创建的节点将失去其所有属性,并且在碰到该节点时无法再触摸/移动或移除它被编程执行的屏幕结束([_enemy removeFromParent])当红色节点跟随绿色节点或反之时,一切正常.只有当相同的颜色继续相同的颜色时.无论如何,我可以创建多个相同节点但让它们每个都是独立的吗?谢谢!

My Problem: Whenever a green node is created after a green node or a red node is created after a red node, the previously created node looses all its attributes and can no longer be touched/moved or be removed when it hits the end of the screen which it is programed to do ([_enemy removeFromParent]) When a red node follows a green node or visversa everything works fine. Its only when the same color proceeds the same color. Is there anyway that I can create multiples of the same node but have them each be independent? Thanks!

附言.说 [_enemies addObject:_enemy] 的行;似乎不起作用,因为它没有将它们添加到数组中.

Ps. the line that says [_enemies addObject:_enemy]; Doesn't seem to work because its not adding them to the array.

@interface MyScene : SKScene <UIGestureRecognizerDelegate,SKPhysicsContactDelegate>
  {

      bool *someBool;

}
@property (strong, nonatomic) SKSpriteNode *wall;

@property (strong, nonatomic) SKSpriteNode *node;
@property (strong, nonatomic) SKSpriteNode *field;
@property (strong, nonatomic) SKSpriteNode *okfield;
@property (strong, nonatomic) SKSpriteNode *board;
@property (strong, nonatomic) SKLabelNode *seconds;
@property (strong, nonatomic) SKSpriteNode *enemy;
@property (strong, nonatomic) SKSpriteNode *enemyy;
@property (strong, nonatomic) SKSpriteNode *enemy1;
@property (strong, nonatomic) SKSpriteNode *enemy2;
@property (strong, nonatomic) SKSpriteNode *enemy3;
@property (strong, nonatomic) SKSpriteNode *enemy4;
@property (strong, nonatomic) SKSpriteNode *enemy5;
@property (strong, nonatomic) SKSpriteNode *enemy6;
@property (nonatomic, strong) TCProgressTimerNode *progressTimerNode1;
@property (nonatomic, strong) TCProgressTimerNode *progressTimerNode2;
@property (nonatomic, strong) TCProgressTimerNode *progressTimerNode3;
@property (nonatomic) NSTimeInterval startTime;




@end
@interface SKEmitterNode (fromFile)
+ (instancetype)orb_emitterNamed:(NSString*)name;
@end





@implementation MyScene{
    BOOL _dead;
    SKNode *_player;
    enum {

    Collisionbordered = 1<<1,
    Collisiongreen = 1<<2,
    Collisionred = 1<<3,
    Collisionbordergreen = 1<<4,


};
NSTimeInterval _startTime;
NSTimer *timerr;
 NSTimer *timer;
int currMinute;
int currSeconds;
SKLabelNode *myLabel;
int i;
int level;
 NSMutableArray *_enemies;
SKAction *go;
SKLabelNode *startlabel;
float changeInX;
float changeInY;
float lastX;
float lastY;
float changeInXX;
float changeInYY;
float lastXX;
float lastYY;}

推荐答案

_enemy 和 _enemyy 变量一次只能指向一个节点.因此,当创建另一个节点时,您无法访问前一个节点.当创建另一个绿色节点时,_enemy 变量指向新节点,而先前创建的节点将无法访问它.您可以使用以下几种方法来访问所有节点:

The _enemy and _enemyy variables can point to only one node at a time. Hence, you cannot access the previous node when another node is created. When another green node is created, the _enemy variable points to the new node and the previously created node loses any way to be able to access it. Here are a few ways you can use to be able to access all nodes:

1 - 正确使用 _enemies 数组.

_enemies 数组不起作用,因为它可能没有在场景开始时初始化.你需要打电话

The _enemies array is not working because it is probably not being initialised at the start of the scene. You need to call

_enemies = [NSMutableArray new];

-initWithSize 方法中.

2 - 为每个节点设置名称

您可以简单地设置每个节点的名称,而不是将每个节点添加到数组中.像这样:

Instead of adding each node to an array, you can simply set the name of each node. Like so:

_enemy.name = @"greenCircle"

然后,要访问所有节点,您可以使用 -enumerateChildNodesWithName: 方法.

Then, to access all nodes you can use the -enumerateChildNodesWithName: method.

这篇关于多个相同节点但没有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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