Need help in creating a memory game(2)
Hi all, for the memory game that I've previously asked for help, I am having some other problems in getting the buttons to be randomize in the correct way.
with this, the buttons are able to randomize and highlight and unhighlight by its own, but for now, it gave me randomize in a fast way. Can I know how to control the randomize speed, as well as to limit how many times it is to be randomize?
Moderator: Please use code tags where appropriate
Code:
-(IBAction) btnClick{
if(gamestate == gamestateRunning){
NSUInteger count = [pattern count];
for(NSUInteger i=0; i<count; i++){
//int n = count-i;
int d = (arc4random()% count) +i;
if(d==1){
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:0.01f];
}
else if(d==2){
[self performSelector:@selector(delayEventBLUE) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseBlue) withObject:nil afterDelay:0.01f];
}
else if(d==3){
[self performSelector:@selector(delayEventGREEN) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseGreen) withObject:nil afterDelay:0.01f];
}
else if(d==4){
[self performSelector:@selector(delayEventYELLOW) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseYellow) withObject:nil afterDelay:0.01f];
}
}
}
}
Moderator: Please use code tags where appropriate
1) just doing int d = (arc4random()% count) is sufficient for randomization, in doing so you would check 0-count.
2) code isn't very robust. it will lead to unpredictable results. If you truly only have 4 options, then: int d = (arc4random()% 4) and then use a switch, 0-3.
now all these are asides from your question: what do you mean by "randomize speed"?
2) code isn't very robust. it will lead to unpredictable results. If you truly only have 4 options, then: int d = (arc4random()% 4) and then use a switch, 0-3.
now all these are asides from your question: what do you mean by "randomize speed"?
(Dec 10, 2010 11:34 AM)skyhawk Wrote: \.
2) code isn't very robust. it will lead to unpredictable results. If you truly only have 4 options, then: int d = (arc4random()% 4) and then use a switch, 0-3.
now all these are asides from your question: what do you mean by "randomize speed"?
Ya, I will need to have 4 options. But will need to have 4 options at the first stage and adding 1 more in subsequent stages.
Now its speed is like haywire as I cannot get the speed of the options right.
That's because you are telling them all to highlight after a delay of 2 seconds. To make them flash at different times you will need to change the delay. Try using i as the delay time.
(Dec 12, 2010 06:40 AM)backslash Wrote: That's because you are telling them all to highlight after a delay of 2 seconds. To make them flash at different times you will need to change the delay. Try using i as the delay time.
it is still the same as i is still considered an integer.
i tried using NSTimer to slow down the random speed but it still went random highlighting of several buttons at a time after 10-13 "self-generated" highlights.
(Dec 13, 2010 12:00 AM)soulesstps Wrote: it is still the same as i is still considered an integer.
OK, use (float)i.
My point is that if you give the same delay to all the buttons, they will all flash at the same time (after the same delay). You need to give a different delay time to each button. I suggested using i because it changes as you iterate through your buttons. performSelector:withObject:afterDelay doesn't block the thread, so you are sending these messages to all the buttons at almost exactly the same time.
I've also just noticed that, for the same reason, you are unhighlighting all your buttons 1.99 seconds before you highlight them, which will presumably make them all light up and stay lit. The delay for the "turn back off" function needs to be "delay until it lights up"+"time it is to stay lit for".
Basically, change this:
To this:
And similar changes to the other lines.
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:0.01f];
To this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f*i];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:2.0f+2*i];
And similar changes to the other lines.
(Dec 13, 2010 05:56 PM)PowerMacX Wrote: Basically, change this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:0.01f];
To this:
Code:
[self performSelector:@selector(delayEventRED) withObject:nil afterDelay:2.0f*i];
[self performSelector:@selector(releaseRed) withObject:nil afterDelay:2.0f+2*i];
And similar changes to the other lines.
hmm. the result shows is still the same. =((
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Need help in the memory game | soulesstps | 7 | 8,669 |
Jan 6, 2011 09:10 PM Last Post: soulesstps |
|
User input for memory game | soulesstps | 0 | 4,097 |
Jan 2, 2011 09:37 PM Last Post: soulesstps |
|
Need help in creating a memory game | soulesstps | 12 | 12,426 |
Dec 4, 2010 11:58 AM Last Post: shazi |
|
creating a game background problems | chrism | 6 | 5,533 |
Jun 6, 2009 06:58 PM Last Post: chrism |
|
Creating an Age-Of-Empires-2-Esque Game for iPhone | Simie | 5 | 7,558 |
Aug 19, 2008 04:35 PM Last Post: SethWillits |