My problem is that I generate a number with random from 0 to 9 to then appear the image with the number that it generate:
Implementation:
@implementation EscenaJuego{
int numberSelected;
int num;
NSString *form;
}
AddImage Number:
-(void)addNumber
{
num = arc4random() % 10;
form = [NSString stringWithFormat:@"%d", num];
SKSpriteNode *numero;
numero = [SKSpriteNode spriteNodeWithImageNamed:form];
[numero setScale:0.5];
......
}
When both bodies touch , do NSLog:
- (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);
}
But then when I do NSLog to get if the number is the same that the image , is different .
Example : When I my character touch 9, NSLog said that is 2.
All image files are their number.png
Edit:
If I do a NSLog in AddNumber method the number is correct with the image:
-(void)addNumber
{
num = arc4random() % 10;
form = [NSString stringWithFormat:@"%d", num];
SKSpriteNode *numero;
numero = [SKSpriteNode spriteNodeWithImageNamed:form];
[numero setScale:0.5];
self.numeroSeleccionado = [form integerValue];
NSLog(@"%@%d", @"Appear:", self.numeroSeleccionado);
......
}
In your code, num
is an instance variable, so it will hold the last value that was set into it. That's what you log. In your code you don't update the variable on contact detection before you use it.
Consider setting the name
of the sprite nodes and using that when contact is detected to determine what actually happened.
Subclass a SKSpriteNode
and create a number property in it. This will simplify your code and make it more clear.
@interface NumberNode : SKSpriteNode
@property(nonatomic, assign) NSUInteger number;
@end
You will not have to have a int num
then. You would just check the number
property of the collided node.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.