iDevGames Forums
Need help in creating a memory game(2) - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Need help in creating a memory game(2) (/thread-8400.html)



Need help in creating a memory game(2) - soulesstps - Dec 10, 2010 01:14 AM

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.
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];
                }
            }
        }
    
}
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


RE: Need help in creating a memory game(2) - skyhawk - Dec 10, 2010 11:34 AM

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"?


RE: Need help in creating a memory game(2) - soulesstps - Dec 10, 2010 12:12 PM

(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.



RE: Need help in creating a memory game(2) - ThemsAllTook - Dec 10, 2010 01:11 PM

(Dec 10, 2010 12:12 PM)soulesstps Wrote:  Now its speed is like haywire as I cannot get the speed of the options right.

This really doesn't give us the information we'd need to help you. What effect are you seeing, and what effect are you trying to achieve?


RE: Need help in creating a memory game(2) - soulesstps - Dec 11, 2010 09:19 PM

(Dec 10, 2010 01:11 PM)ThemsAllTook Wrote:  This really doesn't give us the information we'd need to help you. What effect are you seeing, and what effect are you trying to achieve?

Erm. what I was trying to say is, the buttons that are being randomize did not highlight 1 by 1.


RE: Need help in creating a memory game(2) - backslash - Dec 12, 2010 06:40 AM

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.


RE: Need help in creating a memory game(2) - soulesstps - Dec 13, 2010 12:00 AM

(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.


RE: Need help in creating a memory game(2) - backslash - Dec 13, 2010 06:39 AM

(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".


RE: Need help in creating a memory game(2) - PowerMacX - Dec 13, 2010 05:56 PM

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.


RE: Need help in creating a memory game(2) - soulesstps - Dec 13, 2010 07:44 PM

(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. =((