sprite kit - Spritekit - Spawn waves of monsters -
i'm making simple racing game in monsters spawned (randomly) 3 out of 5 lanes in portrait mode.
-(void)addenemy { skspritenode *enemy; enemy = [skspritenode spritenodewithimagenamed:@"enemy1"]; [enemy setscale:.65]; enemy.physicsbody = [skphysicsbody bodywithrectangleofsize:enemy.size]; enemy.physicsbody.categorybitmask = obstaclecategory; enemy.physicsbody.dynamic = yes; enemy.zposition = 2; enemy.physicsbody.contacttestbitmask = turtlecategory; enemy.physicsbody.collisionbitmask = 0; enemy.physicsbody.usesprecisecollisiondetection = yes; enemy.name = @"enemy"; //selecting random y position enemy int r = (arc4random() % 5) ; enemy.position = cgpointmake(48+r*56,self.frame.size.height); [self addchild:enemy]; skaction *actionmove = [skaction moveto:cgpointmake(enemy.position.x,-80) duration:1.43]; [enemy runaction:actionmove]; skspritenode *enemy2; enemy2 = [skspritenode spritenodewithimagenamed:@"enemy1"]; [enemy2 setscale:.65]; //adding spritekit physicsbody collision detection enemy2.physicsbody = [skphysicsbody bodywithrectangleofsize:enemy.size]; enemy2.physicsbody.categorybitmask = obstaclecategory; enemy2.physicsbody.dynamic = yes; enemy2.zposition = 2; enemy2.physicsbody.contacttestbitmask = turtlecategory; enemy2.physicsbody.collisionbitmask = 0; enemy2.physicsbody.usesprecisecollisiondetection = yes; enemy2.name = @"enemy2"; //selecting random y position enemy int r2 = (arc4random() % 5) ; enemy2.position = cgpointmake(48+r2*56,self.frame.size.height); [self addchild:enemy2]; skaction *actionmove2 = [skaction moveto:cgpointmake(enemy2.position.x,-80) duration:1.43]; [enemy2 runaction:actionmove2]; skspritenode *enemy1; enemy1 = [skspritenode spritenodewithimagenamed:@"boss1"]; [enemy1 setscale:.65]; //adding spritekit physicsbody collision detection enemy1.physicsbody = [skphysicsbody bodywithrectangleofsize:enemy1.size]; enemy1.physicsbody.categorybitmask = obstaclecategory; enemy1.physicsbody.dynamic = yes; enemy1.zposition =2; enemy1.physicsbody.contacttestbitmask = turtlecategory; enemy1.physicsbody.collisionbitmask = 0; enemy1.physicsbody.usesprecisecollisiondetection = yes; enemy1.name = @"enemy1"; int r1 = (arc4random() % 5) ; enemy1.position = cgpointmake(48+r1*56,self.frame.size.height); [self addchild:enemy1]; skaction *actionmove1 = [skaction moveto:cgpointmake(enemy1.position.x,-80) duration:1.43]; [enemy1 runaction:actionmove1];
however, 1 of problem of code monsters spawned on top of each other. there better way this?
- (cgfloat)randomvaluebetween:(cgfloat)low andvalue:(cgfloat)high { return (((cgfloat) arc4random() / 0xffffffffu) * (high - low)) + low; }
i found piece of code raywenderlich tutorial think.
what u can call,
cgfloat r1 = [self randomvaluebetween:0 andvalue:3]; cgfloat r2 = [self randomvaluebetween:3 andvalue:6]; etc
that might give different values u can use offset.
Comments
Post a Comment