Scoring, particularly health!

Discussion in 'iOS Programming' started by joeysefika, Oct 3, 2009.

  1. joeysefika macrumors newbie

    Joined:
    Sep 26, 2009
    #1
    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?
     
  2. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #2
    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
     
  3. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #3
    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.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
  5. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #5
    www.71squared.com

    any book will do it but that link has video tutorials and will be useful for you to see how the code is laid out
     
  6. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #6
    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!
     
  7. joeysefika thread starter macrumors newbie

    Joined:
    Sep 26, 2009
    #7
    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
     
  8. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #8
    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)
     

Share This Page