I have an NSMutableArray (named coins) that isn’t following a randomPoint method. When I go to iOS Simulator, the coins don’t show up (as in there are no images) and they stay in the center of the screen instead of a randomly assigned point. This is what should make a new coin:
if ([coins count] == 0){
CGSize windowSize = [CCDirector sharedDirector].winSize;
CGPoint randomPointOnScreen = ccp((float)(arc4random() % 100) / 100 * windowSize.width, (float)(arc4random() % 100) / 100 * windowSize.height);
randomCoinType = arc4random() % kNumberOfCoinTypes + 1; // I have this because my files are named like "coin.1.png" and such
kNumberOfCoinTypes is defined as 10 to give me a number 1-10.
This is the corresponding void that should make the coins:
-(void)createCoinsAt:(CGPoint)position withCointype:(int)type{
NSString *imageFile;
switch (type) {
case 1:
imageFile = @"coin.1.png";
break;
case 2:
imageFile = @"coin.2.png";
break;
case 9:
case 3:
imageFile = @"coin.3.png";
break;
case 7:
case 8:
default:
case 4:
imageFile = @"coin.4.png";
break;
case 5:
imageFile = @"coin.5.png";
break;
case 6:
imageFile = @"coin.6.png";
break;
}
Coins *c = [Coins spriteWithFile:imageFile];
//Coins *c = [Coins spriteWithFile:[NSString stringWithFormat:@"coin.%d.png", arc4random() % 5 + 1 ]];
c.type = type;
c.position = position;
c.velocity = ccp(0,0);
[coins addObject:c];
[self addChild:c z:2];
}
I have multiple cases following one command because I don’t know how to make one number appear more often than another.
Everything is fine with the code you’ve shown. Your problems are probably in your
Coinclass.Things to check in your
Coinclass:velocityto {0,0} inadvertently changeposition?CCSpritemethods have you overridden? Make sure to call thesuperof those methods.Other things to try:
coin.1.pngvisibly appear at a random point using only theCCSpriteclass? If not…coin.1.pngfile is correctly added to your project.UPDATE: Avoiding subclassing CCSprite
—
—
Once a Coin is given a velocity:
Once a Coin disappears/dies/whatever: