随机和数字图像不是';相同的iOS xCode

随机和数字图像不是';相同的iOS xCode,ios,objective-c,Ios,Objective C,我的问题是,我生成了一个从0到9的随机数,然后用它生成的数字显示图像: 实施: @implementation EscenaJuego{ int numberSelected; int num; NSString *form; } -(void)addNumber { num = arc4random() % 10; form = [NSString stringWithFormat:@"%d", num]; SKSpriteNode *

我的问题是,我生成了一个从0到9的随机数,然后用它生成的数字显示图像:

实施:

@implementation EscenaJuego{
    int numberSelected;
    int num;
    NSString *form;    
}
-(void)addNumber
{
    num = arc4random() % 10;
    form = [NSString stringWithFormat:@"%d", num];
    SKSpriteNode *numero;
    numero = [SKSpriteNode spriteNodeWithImageNamed:form];
    [numero setScale:0.5];
    ......
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *primerCuerpo, *segundoCuerpo;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        primerCuerpo = contact.bodyA;
        segundoCuerpo = contact.bodyB;
    }
    else
    {
        primerCuerpo = contact.bodyB;
        segundoCuerpo = contact.bodyA;
    }


    if ((primerCuerpo.categoryBitMask & categoriaPersonaje) != 0 &&
        (segundoCuerpo.categoryBitMask & categoriaMovimiento) != 0)
    {
        numberSelected = num;
        NSLog(@"%d", num);        
    }
添加图像编号:

@implementation EscenaJuego{
    int numberSelected;
    int num;
    NSString *form;    
}
-(void)addNumber
{
    num = arc4random() % 10;
    form = [NSString stringWithFormat:@"%d", num];
    SKSpriteNode *numero;
    numero = [SKSpriteNode spriteNodeWithImageNamed:form];
    [numero setScale:0.5];
    ......
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *primerCuerpo, *segundoCuerpo;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        primerCuerpo = contact.bodyA;
        segundoCuerpo = contact.bodyB;
    }
    else
    {
        primerCuerpo = contact.bodyB;
        segundoCuerpo = contact.bodyA;
    }


    if ((primerCuerpo.categoryBitMask & categoriaPersonaje) != 0 &&
        (segundoCuerpo.categoryBitMask & categoriaMovimiento) != 0)
    {
        numberSelected = num;
        NSLog(@"%d", num);        
    }
当两个身体接触时,请记录:

@implementation EscenaJuego{
    int numberSelected;
    int num;
    NSString *form;    
}
-(void)addNumber
{
    num = arc4random() % 10;
    form = [NSString stringWithFormat:@"%d", num];
    SKSpriteNode *numero;
    numero = [SKSpriteNode spriteNodeWithImageNamed:form];
    [numero setScale:0.5];
    ......
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *primerCuerpo, *segundoCuerpo;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        primerCuerpo = contact.bodyA;
        segundoCuerpo = contact.bodyB;
    }
    else
    {
        primerCuerpo = contact.bodyB;
        segundoCuerpo = contact.bodyA;
    }


    if ((primerCuerpo.categoryBitMask & categoriaPersonaje) != 0 &&
        (segundoCuerpo.categoryBitMask & categoriaMovimiento) != 0)
    {
        numberSelected = num;
        NSLog(@"%d", num);        
    }

在您的代码中,
num
是一个实例变量,因此它将保存设置到其中的最后一个值。这就是你记录的。在代码中,在使用接触检测变量之前,不会更新该变量


考虑设置精灵节点的
名称
,并在检测到接触时使用该名称来确定实际发生的情况。

子类a
SKSpriteNode
,并在其中创建一个number属性。这将简化您的代码并使其更加清晰

@interface NumberNode : SKSpriteNode
@property(nonatomic, assign) NSUInteger number;
@end

那么您就不必拥有
int num
。您只需检查冲突节点的
number
属性。

code您发布了有关nslog的代码?为什么
num
form
实例变量?您可以看到我的编辑问题我已经做了,但它做的是相同的。。。在AddNumber方法中:
self.number=[formintegervalue]在问题中显示更新的代码。删除num和form实例变量。我应该怎么做?使用属性?我删除了实例变量(num和form),使用属性也删除了实例变量,但效果相同