I'm currently working in SpriteKit in Objective C, and I'm trying to set a number of random textures in a switch statement to 9 different SKSpriteNodes. Currently, this section of my code looks like this:
int randomCard = arc4random_uniform(13);
switch (randomCard)
{
case 0:
staveCard = [SKTexture textureWithImageNamed:#"Card_C.png"];
break;
case 1:
staveCard = [SKTexture textureWithImageNamed:#"Card_D.png"];
break;
case 2:
staveCard = [SKTexture textureWithImageNamed:#"Card_E.png"];
break;
case 3:
staveCard = [SKTexture textureWithImageNamed:#"Card_F.png"];
break;
case 4:
staveCard = [SKTexture textureWithImageNamed:#"Card_G.png"];
break;
case 5:
staveCard = [SKTexture textureWithImageNamed:#"Card_A.png"];
break;
case 6:
staveCard = [SKTexture textureWithImageNamed:#"Card_B.png"];
break;
case 7:
staveCard = [SKTexture textureWithImageNamed:#"Card_C2.png"];
break;
case 8:
staveCard = [SKTexture textureWithImageNamed:#"Card_D2.png"];
break;
case 9:
staveCard = [SKTexture textureWithImageNamed:#"Card_E2.png"];
break;
case 10:
staveCard = [SKTexture textureWithImageNamed:#"Card_F2.png"];
break;
case 11:
staveCard = [SKTexture textureWithImageNamed:#"Card_G2.png"];
break;
case 12:
staveCard = [SKTexture textureWithImageNamed:#"Card_A2.png"];
break;
}
faceDownStaveCard1 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard2 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard3 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard4 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard5 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard6 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard7 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard8 = [SKSpriteNode spriteNodeWithTexture:staveCard];
faceDownStaveCard9 = [SKSpriteNode spriteNodeWithTexture:staveCard];
When I run this on the simulator, all of the sprite nodes display the same random texture. However, I want the textures to be random for each sprite node, picking a different node each time.
Something like:
pick random texture from switch statement
add to first sprite node
pick random texture from switch statement
add to second sprite node
etc.
Is anyone able to help with this?
Your program's flow now is:
Create random number
Pick 1 texture
Set that texture to every node
It should loop the first 2 steps the same amount of times as nodes present. Try something like:
for (int i = 0; i<9; i++) {
int randomCard = arc4random_uniform(13);
switch (randomCard)
{
case 0:
staveCard = [SKTexture textureWithImageNamed:#"Card_C.png"];
break;
case 1:
staveCard = [SKTexture textureWithImageNamed:#"Card_D.png"];
break;
case 2:
staveCard = [SKTexture textureWithImageNamed:#"Card_E.png"];
break;
case 3:
staveCard = [SKTexture textureWithImageNamed:#"Card_F.png"];
break;
case 4:
staveCard = [SKTexture textureWithImageNamed:#"Card_G.png"];
break;
case 5:
staveCard = [SKTexture textureWithImageNamed:#"Card_A.png"];
break;
case 6:
staveCard = [SKTexture textureWithImageNamed:#"Card_B.png"];
break;
case 7:
staveCard = [SKTexture textureWithImageNamed:#"Card_C2.png"];
break;
case 8:
staveCard = [SKTexture textureWithImageNamed:#"Card_D2.png"];
break;
case 9:
staveCard = [SKTexture textureWithImageNamed:#"Card_E2.png"];
break;
case 10:
staveCard = [SKTexture textureWithImageNamed:#"Card_F2.png"];
break;
case 11:
staveCard = [SKTexture textureWithImageNamed:#"Card_G2.png"];
break;
case 12:
staveCard = [SKTexture textureWithImageNamed:#"Card_A2.png"];
break;
}
switch (i)
{
case 0:
faceDownStaveCard1 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 1:
faceDownStaveCard2 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 2:
faceDownStaveCard3 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 3:
faceDownStaveCard4 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 4:
faceDownStaveCard5 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 5:
faceDownStaveCard6 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 6:
faceDownStaveCard7 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 7:
faceDownStaveCard8 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
case 8:
faceDownStaveCard9 = [SKSpriteNode spriteNodeWithTexture:staveCard];
break;
}
}
This is just an idea of how the flow should be, please optimize the code. Good luck!
Related
In my sprite kit game, nodes are endlessly moving up the screen, however, when they first appear on the screen the game lags and the fps fluctuates. After several seconds the nodes start to move smoothly, however, at times their movement is still fidgety. I believe that there is a problem with my update methods, however I am unsure of how to structure them in a better way. Below are my update methods. Any help will be much appreciated, thanks in advance
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval >= increment) {
self.lastSpawnTimeInterval = 0;
int randomNumber = arc4random_uniform(24);
switch (randomNumber) {
case 1:
[self addChild: [self obstacle1]];
[self addChild: [self obstacle5]];
break;
case 2:
[self addChild: [self obstacle1]];
[self addChild: [self obstacle6]];
break;
case 3:
[self addChild: [self obstacle1]];
[self addChild: [self obstacle7]];
break;
case 4:
[self addChild: [self obstacle1]];
[self addChild: [self obstacle8]];
break;
case 5:
[self addChild: [self obstacle2]];
[self addChild: [self obstacle5]];
break;
case 6:
[self addChild: [self obstacle2]];
[self addChild: [self obstacle6]];
break;
case 7:
[self addChild: [self obstacle2]];
[self addChild: [self obstacle7]];
break;
case 8:
[self addChild: [self obstacle2]];
[self addChild: [self obstacle8]];
break;
case 9:
[self addChild: [self obstacle3]];
[self addChild: [self obstacle5]];
break;
case 10:
[self addChild: [self obstacle3]];
[self addChild: [self obstacle6]];
break;
case 11:
[self addChild: [self obstacle3]];
[self addChild: [self obstacle7]];
break;
case 12:
[self addChild: [self obstacle3]];
[self addChild: [self obstacle8]];
break;
case 13:
[self addChild: [self obstacle4]];
[self addChild: [self obstacle5]];
break;
case 14:
[self addChild: [self obstacle4]];
[self addChild: [self obstacle6]];
break;
case 15:
[self addChild: [self obstacle4]];
[self addChild: [self obstacle7]];
break;
case 16:
[self addChild: [self obstacle1]];
break;
case 17:
[self addChild: [self obstacle2]];
break;
case 18:
[self addChild: [self obstacle3]];
break;
case 19:
[self addChild: [self obstacle4]];
break;
case 20:
[self addChild: [self obstacle5]];
break;
case 21:
[self addChild: [self obstacle6]];
break;
case 22:
[self addChild: [self obstacle7]];
break;
case 23:
[self addChild: [self obstacle8]];
break;
default:
[self addChild: [self obstacle4]];
[self addChild: [self obstacle8]];
break;
}
}
}
- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast >= 1) { // more than a second since last update
timeSinceLast = 1 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}
[self updateWithTimeSinceLastUpdate:timeSinceLast];
}
The increment integer I have implemented varies between 1.0 and 1.9 and gradually decreases as game continues.
Below is one of my obstacle methods. All obstacle methods are relatively similar. The only differences are position and image.
- (SKSpriteNode *)obstacle1 {
int placement[6] = {0, 50, 100, 150, 200, 250};
int RandomPlacement = placement[(arc4random()%6)];
NSArray *obstacle1Images;
SKSpriteNode *obstacle1Node;
obstacle1Node = [SKSpriteNode spriteNodeWithImageNamed:#"dodgR - spike left iPad.png"];
obstacle1Node.position = CGPointMake(CGRectGetMidX(self.frame) - 321.5, CGRectGetMidY(self.frame)-self.frame.size.width/2 - RandomPlacement);
obstacle1Node.size = CGSizeMake(292, 95);
obstacle1Images = #[[SKTexture textureWithImageNamed:#"dodgR - spike left iPad.png"],
[SKTexture textureWithImageNamed:#"dodgR - spike left iPad; 1.png"],
[SKTexture textureWithImageNamed:#"dodgR - spike left iPad; 2.png"],
[SKTexture textureWithImageNamed:#"dodgR - spike left iPad; 3.png"],
];
SKAction *obstacle1Animation = [SKAction animateWithTextures:obstacle1Images timePerFrame:0.2];
SKAction *obstacle1Repeat = [SKAction repeatActionForever:obstacle1Animation];
[obstacle1Node runAction:obstacle1Repeat];
SKNode *person1 = [self childNodeWithName:#"person1"];
if (person1.position.y > obstacle1Node.position.y) {
[self ScoreTing];
}
i made a simple drawing App, with a toolpanel which contains a pencil, a paint brush and an eraser. There a 12 different colors to draw with.
Now, i would like the pencil and the paint brush image, change in the color of the active color from the colorpanel. How do I do that?
Something like;
When toolbutton:pencil is pressed, color pencil (image) is color:changecolor.
How to translate this in code?
This is my code for the buttons.
- (IBAction)colorChange:(id)sender
{
UIButton *PressedButton = (UIButton*)sender;
switch (PressedButton.tag) {
case 0:
self.drawingView.lineColor = RGB(32, 152, 188);
break;
case 1:
self.drawingView.lineColor = RGB(33, 114, 177);
break;
case 2:
self.drawingView.lineColor = RGB(65, 79, 155);
break;
case 3:
self.drawingView.lineColor = RGB(110, 58, 141);
break;
case 4:
self.drawingView.lineColor = RGB(195, 26, 126);
break;
case 5:
self.drawingView.lineColor = RGB(228, 36, 40);
break;
case 6:
self.drawingView.lineColor = RGB(234, 98, 36);
break;
case 7:
self.drawingView.lineColor = RGB(241, 141, 33);
break;
case 8:
self.drawingView.lineColor = RGB(252, 199, 19);
break;
case 9:
self.drawingView.lineColor = RGB(255, 240, 79);
break;
case 10:
self.drawingView.lineColor = RGB(139, 188, 63);
break;
case 11:
self.drawingView.lineColor = RGB(0, 144, 92);
break;
}
}
- (IBAction)toolChange:(id)sender
{
UIButton *toolButton = (UIButton*)sender;
switch (toolButton.tag){
case 0:
self.drawingView.drawTool = ToolTypePen;
self.drawingView.lineWidth = 7;
self.drawingView.lineAlpha = 1.0;
self.potlood02.hidden=NO;
self.kwast02.hidden=YES;
self.gum02.hidden=YES;
break;
case 1:
self.drawingView.drawTool = ToolTypePen;
self.drawingView.lineWidth = 20;
self.drawingView.lineAlpha = 0.67;
self.potlood02.hidden=YES;
self.kwast02.hidden=NO;
self.gum02.hidden=YES;
break;
case 6:
self.drawingView.drawTool = ToolTypeEraser;
self.drawingView.lineWidth = 20;
self.potlood02.hidden=YES;
self.kwast02.hidden=YES;
self.gum02.hidden=NO;
break;
}
}
To change the colour of your pencil image you could do something like this
UIImage *originalImage = [UIImage imageNamed:#"PencilImage.png"];
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your pencil image size
imageView.tintColor = [UIColor redColor]; // or whatever color that has been selected
imageView.image = newImage;
Hope this helps.
I am making an app that has 2 UIImageViews but in each image view the image changes 3 times. How can I make it so that if image A collides with image B or C action happens but not with image A and so on. Here is the code for the image changing
ImageView Changing 1
This is a random change
Trap.center = CGPointMake(350,220);
Trap1 = rand() %3;
switch (Trap1) {
case 0:
Trap.image = [UIImage imageNamed:#"Image1.png"];
break;
case 1:
Trap.image = [UIImage imageNamed:#"Image2.png"];
break;
case 2:
Trap.image = [UIImage imageNamed:#"Image3.png"];
break;
default:
break;
}
ImageView Changing 2
This is set with a swipe and is copied for the other 2 changes.
-(IBAction)Change3:(id)sender{
UIImage *img = [UIImage imageNamed:#"Image4.tif"];
[Change3 setImage:img];
}
You can add a tag to your Trap object. Compare the tags and if they are different, do something.
Set tag:
switch (Trap1) {
case 0:
Trap.image = [UIImage imageNamed:#"Image1.png"];
Trap.tag = 1;
break;
case 1:
Trap.image = [UIImage imageNamed:#"Image2.png"];
Trap.tag = 2;
break;
case 2:
Trap.image = [UIImage imageNamed:#"Image3.png"];
Trap.tag = 3;
break;
default:
break;
}
Compare tags
If (trap1.tag != trap2.tag){
//Do something
}
I have 4 function and i want to execute them randomly every 1 second for unlimited time.
This is my code to execute a random function:
-(void) Game
{
int rand = arc4random() % 5;
switch (rand) {
case 0:
[self appearred];
break;
case 1:
[self appearblue];
break;
case 2:
[self appearyellow];
break;
case 3:
[self appeargreen];
break;
}
}
But how can i repeat this function every 1 second??
You can also try like this
*
-(void) Game
{
int rand = (int)arc4random() % 4;
switch (rand) {
case 0:
[self appearred];
break;
case 1:
[self appearblue];
break;
case 2:
[self appearyellow];
break;
case 3:
[self appeargreen];
break;
}
[self performSelector:#selector(Game) withObject:nil afterDelay:1.0];
}
*
It will call the Game Functions after each second
Just use a NSTimer.
PS : You will not be 100% sure that this will append every 1second.
Use below code which will call Game method every 1.0 second. One more thing, in your Game method you need to use % 4 instead of % 5 to get value between 0 to 3.
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(Game)
userInfo:nil
repeats:YES];
-(void) Game
{
int rand = (int)arc4random() % 4;
switch (rand) {
case 0:
[self appearred];
break;
case 1:
[self appearblue];
break;
case 2:
[self appearyellow];
break;
case 3:
[self appeargreen];
break;
}
}
How can i change the Color of my Picker view depending on the background color of the TextField? e.g i have 2 text fields, The one is red and the other one is blue, If i clicked the red one the picker background is red also, And if i click the blue one the picker background will be blue also? im using this code for the text field to show the picker view.
int tag = [(UITextField*)sender tag];
self.myPicker.hidden = NO;
selectedTable = tag;
[sender resignFirstResponder];
float yy = 10;
switch (tag) {
case 0: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
break;
case 1: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
break;
case 2: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
break;
case 3: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
break;
case 4: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
break;
case 5: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
break;
case 6: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
break;
case 7: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
break;
case 8: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
break;
default:
break;
}
thank you!
use self.picker.backgroundColor = [UIColor redColor]; in textFieldDidBeginEditing: UITextFieldDelegate method and change color using by using tag property.
if(textField.tag == 1){
self.picker.backgroundColor = [UIColor redColor];
}
else if(textField.tag == 2){
self.picker.backgroundColor = [UIColor redColor];
}
else{
//use default value
}
Edit:
switch (tag) {
case 0: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
self.picker.backgroundColor = [UIColor redColor];
break;
case 1: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
self.picker.backgroundColor = [UIColor bluecolor];
break;
case 2: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
self.picker.backgroundColor = [UIColor greenColor];
break;
case 3: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
break;
case 4: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
break;
case 5: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
break;
case 6: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
break;
case 7: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
break;
case 8: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
break;
default:
break;
}
add different background colour for each case