Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

joeysefika

macrumors newbie
Original poster
Sep 26, 2009
5
0
So i have some simple code with a space invaders-esque design. I have implemented code that causes the score to start at 100 then loose 1 point when hit by a missile. I have 2 questions.

1, this code here
Code:
}
	playerScore.text = [NSString stringWithFormat:@"%d",playerScore_value];
	playerScore_value = 100;
	

	
	if(CGRectIntersectsRect(SpaceJunk.frame,player.frame)) {
		if(SpaceJunk.center.y > player.center.y) {
			playerScore_value--;
	}
	}
	if (playerScore_value < 100){
		[playerScore setTextColor: [UIColor greenColor]];
	}
	
	if (playerScore_value < 50){
		[playerScore setTextColor: [UIColor yellowColor]];
	}
	
	if (playerScore_value < 20){
		[playerScore setTextColor: [UIColor redColor]];
	}
	
	if (playerScore_value < 5){
		[playerScore setTextColor: [UIColor blackColor]];
	}
}

is the code for scoring, as you can see playerScore_value is set at 100, when the 'SpaceJunk' hits the ship it looses 1 point, all well and good
However as soon as the 'SpaceJunk' leaves the proximity of the player the health goes back up to 100! how can i prevent this? Also how do i make it so it take 5 off each time it hit, i.e. where i have
Code:
playerScore_value--;
the "--" takes off one, is there a way where i can have "-5"?

Thanks

2,
My second question is to do with the spawning of the SpaceJunk itself, i have the being initialised on game start off screen and at a random X variable
Code:
	int randomX = arc4random() % 320 + 1;
	
	SpaceJunkImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"flash1.png"], [UIImage imageNamed:@"flash3.png"], [UIImage imageNamed:@"flash4.png"], [UIImage imageNamed:@"flash5.png"], [UIImage imageNamed:@"flash2.png"], nil];
	SpaceJunk = [[UIImageView alloc] initWithFrame: CGRectMake (randomX, 0, 0, 0)];
	SpaceJunk.animationDuration = 0.5;
	SpaceJunk.contentMode = UIViewContentModeBottomLeft;
	SpaceJunk.animationImages = SpaceJunkImages;
	[SpaceJunk startAnimating];
	[self.view addSubview:SpaceJunk];

How can i make it spawn over and over again, so there's multiple things coming down?
 
ok for starters

playerScore_value -= 5;
or
playerScore_value = playScore_value - 5;

playerScore.text = [NSString stringWithFormat:mad:"%d",playerScore_value];
playerScore_value = 100;
This, will show the current playerscore_value and then you set it to 100, which makes me think earlier in your program you set this to 100?

When you initialise your objects, that is where you set playerscore_value to 100 and then you don't need to set it to 100 again. So remove the playerscore_value = 100; and put it near the start of your program.

IMO, for the second question
create a class for spaceJunk which stores all that information, (means you don't have to copy and paste it anywhere)
and then create an NSMutableArray and add each spacejunk to the array,
so you could add 3 spaceJunk to the array, then it would loop through the array and display all of them

Then you loop through and if they off the bottom of the screen, delete that item in the array, and then you can also add new items to the array which can then be displayed on the screen

This isn't a 'quick fix' but in my opinion it will benefit you in the long run by teaching you good object oriented programming as well as making your game easier to understand and later modify
 
Can you link me to a good guide how to create a class? I'm not a beginner but I never found the need to learn how. I think i should.
 
ok, so if you want a quick work around, incase you are just doing this for a bit of fun and don't want to go away and learn about classes (although i suggest you should)
you could do something like this:
Code:
SpaceJunkImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"flash1.png"], [UIImage imageNamed:@"flash3.png"], [UIImage imageNamed:@"flash4.png"], [UIImage imageNamed:@"flash5.png"], [UIImage imageNamed:@"flash2.png"], nil];

int randomX1 = arc4random() % 320 + 1;

	SpaceJunk1 = [[UIImageView alloc] initWithFrame: CGRectMake (randomX1, 0, 0, 0)];
	SpaceJunk1.animationDuration = 0.5;
	SpaceJunk1.contentMode = UIViewContentModeBottomLeft;
	SpaceJunk1.animationImages = SpaceJunkImages;
	[SpaceJunk1 startAnimating];
	[self.view addSubview:SpaceJunk1];

int randomX2 = arc4random() % 320 + 1;

	SpaceJunk2 = [[UIImageView alloc] initWithFrame: CGRectMake (randomX2, 0, 0, 0)];
	SpaceJunk2.animationDuration = 0.5;
	SpaceJunk2.contentMode = UIViewContentModeBottomLeft;
	SpaceJunk2.animationImages = SpaceJunkImages;
	[SpaceJunk2 startAnimating];
	[self.view addSubview:SpaceJunk2];

This is a bad way to do it, but atleast if you did do it this way then take the time to learn about classes you will very quickly realise why it can be so great to use classes!
 
Thanks everyone! Especially Kingbombs... just one more issue

So now I have health being taken off every time the character is hit and a new instance of 'SpaceJunk' being drawn at the top of the screen. However when the ship is hit with the 'SpaceJunk' the 'SpaceJunk' just sits there stationary on screen where it intersected with the ship. How do i get rid of the SpaceJunk?

i.e. SpaceJunk= [[UIImageView alloc] initWithFrame: CGRectMake(0,0,0,0)];

obviously i'm hoping for a more practical way to do this than the above code
 
So now I have health being taken off every time the character is hit and a new instance of 'SpaceJunk' being drawn at the top of the screen. However when the ship is hit with the 'SpaceJunk' the 'SpaceJunk' just sits there stationary on screen where it intersected with the ship. How do i get rid of the SpaceJunk?

i.e. SpaceJunk= [[UIImageView alloc] initWithFrame: CGRectMake(0,0,0,0)];

obviously i'm hoping for a more practical way to do this than the above code

i don't think this works but maybe:
[self.view removeFromSuperview: spaceJunk];
or
[self.view removefromSubView:SpaceJunk];
not sure if they will work but you might want to check apple documentation

the other option is
to loop through all the subviews something like for (Subviews *sub in self.subviews)
if sub == spaceJunk
// etc

something like that, it might be sulf.view.subviews not sure

EDIT:: you just have to find a way to remove it from the view, if you just make it CGRect(0,0) then it will still be there and you can't re-use it
one way might be to try and put it at the top again but you would have to modify the subview again (which would probably require looping through and trying to find which one it is)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.