jeudi 1 janvier 2015

Obj-C and SpriteKit - Changing a sprite value that is created randomly

I'm making a game using SpriteKit and Objective-C.


I have four different texture drops (Blue, Green, Orange and Red) that falls down on screen randomly.


In my ANBDropNode class I have this method:



+(instancetype)dropOfType:(ANBDropType)type {

ANBDropsNode *drop;

if (type == ANBDropTypeBlue) {
drop = [self spriteNodeWithImageNamed:@"bluedrop"];
} else if (type == ANBDropTypeGreen) {
drop = [self spriteNodeWithImageNamed:@"greendrop"];
} else if (type == ANBDropTypeOrange) {
drop = [self spriteNodeWithImageNamed:@"orangedrop"];
} else if (type == ANBDropTypeRed){
drop = [self spriteNodeWithImageNamed:@"reddrop"];
}

[drop setupPhysicsBody];
return drop;
}


And in my GamePlayScene these two:



-(void)addDrops {

NSUInteger randomDrop = [ANBUtil randomWithMin:0 max:4];

self.drop = [ANBDropsNode dropOfType:randomDrop];

float y = self.frame.size.height + self.drop.size.height;
float x = [ANBUtil randomWithMin:10 + self.drop.size.width
max:self.frame.size.width - self.drop.size.width - 10];
self.drop.position = CGPointMake(x, y);

[self addChild:self.drop];
}

-(void)update:(NSTimeInterval)currentTime {

if (self.lastUpdateTimeInterval) {
self.timeSinceDropAdded += currentTime - self.lastUpdateTimeInterval;
}

if (self.timeSinceDropAdded > 1) {
[self addDrops];
self.timeSinceDropAdded = 0;
}

self.lastUpdateTimeInterval = currentTime;

}


The question is (and it may sound a little dumber, I know): before the drop hits the ground it has already changed it value. If ANBDropNode *drop is a bluedrop, before it hits the ground the method randomly create another drop and change it value for greendrop, for example. But I don't want this behavior. I want the drop to continue with its value until it reaches the ground so I can detect its color in my didBeginContact method.





Aucun commentaire:

Enregistrer un commentaire