Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 03:07 PM   #1 (permalink)
Registered Member
 
2WeeksToGo's Avatar
 
Join Date: Nov 2011
Posts: 227
2WeeksToGo is on a distinguished road
Default A little complex arc4random to object question.

Hi, I have been stuck on this for a few days now trying different things but to no avail.

I want to generate a number with arc4random and then make that number save in my PowerRandom int. Then make JUST the name PowerRandom one of my objects name in my game, and then use that name that the number is set on to call an object. Its hard to explain I will just input the code.

Code:
 if(Cyclelabel == 0) {
        
        PowerRandom = arc4random() % 5;
        lblPowerRandom.text = [NSString stringWithFormat:@"%d", PowerRandom];
        
    }
 
    if(PowerRandom == 1) {
        PowerRandom == pad1;
    }

   if(PowerRandom == 2) {
        PowerRandom == pad2;
    }

   if(PowerRandom == 3) {
        PowerRandom == pad3;
    }

AND SO On...
So in the above when my timer hits 0, a random number is generated and the PowerRandom int becomes that number that gets transfered to the label.

Then I check if PowerRandom is 2 for example, and if it is, that name PowerRandom becomes a substitute for THE NAME pad2 which is a Object.

The above gives me an error "Comparison Between points and integer" Yellow.

THEN what i want to do this:

Code:
piece.center = CGPointMake(PowerRandom.center.x,PowerRandom.center.y - 25);
So whatever PowerRandom becomes, it is used to be either pad 1 2 3 4 and so on.. and then all i need is that one line of code to place my piece which is a power up. Im sorry if this is a bit complicated, i tried to explain as best as i can. Anyone Know how to do this?
2WeeksToGo is offline   Reply With Quote
Old 11-21-2011, 04:13 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by 2WeeksToGo View Post
Hi, I have been stuck on this for a few days now trying different things but to no avail.

I want to generate a number with arc4random and then make that number save in my PowerRandom int. Then make JUST the name PowerRandom one of my objects name in my game, and then use that name that the number is set on to call an object. Its hard to explain I will just input the code.

Code:
 if(Cyclelabel == 0) {
        
        PowerRandom = arc4random() % 5;
        lblPowerRandom.text = [NSString stringWithFormat:@"%d", PowerRandom];
        
    }
 
    if(PowerRandom == 1) {
        PowerRandom == pad1;
    }

   if(PowerRandom == 2) {
        PowerRandom == pad2;
    }

   if(PowerRandom == 3) {
        PowerRandom == pad3;
    }

AND SO On...
So in the above when my timer hits 0, a random number is generated and the PowerRandom int becomes that number that gets transfered to the label.

Then I check if PowerRandom is 2 for example, and if it is, that name PowerRandom becomes a substitute for THE NAME pad2 which is a Object.

The above gives me an error "Comparison Between points and integer" Yellow.

THEN what i want to do this:

Code:
piece.center = CGPointMake(PowerRandom.center.x,PowerRandom.center.y - 25);
So whatever PowerRandom becomes, it is used to be either pad 1 2 3 4 and so on.. and then all i need is that one line of code to place my piece which is a power up. Im sorry if this is a bit complicated, i tried to explain as best as i can. Anyone Know how to do this?


You don't say what kind of variable PowerRandom is.

it looks to me like it's a CGPoint, judging by the other code you posted.

You can't assign an integer to a CGPoint, and you can't compare an integer to a point. A CGPoint is a structure that contains an x and a y value.


Change your code to use a randomIndex to decide which pad value to assign to powerRandom:


Code:
 if(Cyclelabel == 0) {

        int randIndex =  arc4random() % 5+1;    
        lblPowerRandom.text = [NSString stringWithFormat:@"%d", randIndex];
        
    }
 
    switch (randIndex)
    {
      case 1:
        PowerRandom = pad1;
        break;
      case 2:
        PowerRandom = pad2;
        break;
      case 3:
        PowerRandom = pad3;
        break;
      case 4:
        PowerRandom = pad4;
        break;
      case 5:
        PowerRandom = pad5;
        break;
    }
By the way, variable names should always start with a lower case letter. Only class names should start with an upper case letter. That's a very strong convention in Cocoa programming. Learn to follow it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 11-21-2011 at 05:47 PM.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 05:00 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

you are using double = (==) as an assignment which doesn't make sense:

PowerRandom == pad1;

that line above means nothing.
RickSDK is offline   Reply With Quote
Old 11-21-2011, 05:47 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
you are using double = (==) as an assignment which doesn't make sense:

PowerRandom == pad1;

that line above means nothing.
Oops! I edited the OPs code, and didn't catch that. I fixed it in my post above. Thanks for keeping me honest.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 06:37 PM   #5 (permalink)
Registered Member
 
2WeeksToGo's Avatar
 
Join Date: Nov 2011
Posts: 227
2WeeksToGo is on a distinguished road
Default

Thanx a lot to the both of you, I will try this later tonight and let you know
2WeeksToGo is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
15 members and 375 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:34 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0