Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8928549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:27:50+00:00 2026-06-15T08:27:50+00:00

I need some help (duh). I want to generate a random Y position for

  • 0

I need some help (duh). I want to generate a random Y position for my game in cocos2d.

So the situation goes like this:
The game spawns a platform every 0.2 second. The iPhone / iPad is in landscape mode. Platform appear on the right of the screen (bigger x than width so that the platform appears outside the screen) and starts moving towards the left end of the screen using CCMoveTo.
I pick a random Y for every platform. The problem is that I do not want to spawn a platform on top of another. This means that I need to make a randY which is not “already taken”.
The code I’ve tried so far is this:

//this is a part of code from my addPlatform function. This part in particular cares for the generation of my Y coordinate.
int randY = arc4random() % (int)(3 * (winSize.height/4)); //This prevents to spawn a Y larger than 3/4 of the screen

//here I would like to loop long enough to find a good Y
while (![self isGoodPlatformY:randY])
{
    randY = arc4random() % (int)(3 * (winSize.height/4));
}

The next part is my isGoodPlatformY function

- (bool)isGoodPlatformY:(int)platY
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    int padding = 100;
    bool ok = true;
    for (CCSprite *body in [self children])
    {
        if (body.tag > platformBody)
        {
            if (body.position.x < (winSize.width - padding))
            {
                if (abs(body.position.y - platY) < 20)
                {
                    ok = false;
                }
            }
        }
    }
return ok;
}

I loop through all the bodies with larger tag than my platform. I have different types of platform which I separate by using tag. If the body is a platform I first check the X coordinate. If the platform is enough away (padding) I can spawn a new one at that exact point so I check the next one. If not I want to check the Y of that platform. If the Y coordinate is less than 20 pixels in this case I must find a new Y so thats why set the bool to false and return it after the for loop.
I know there is no need for those curly brackets but I was testing some other stuff, thats why I put them there.

This doesn’t seem to work. Hope I made myself clear what I want to accomplish. Any help from your side would be much appreciated. Hope I didn’t miss something too newbie style 🙂

I tagged the question in other languages to because this problem could occur “everywhere”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:27:52+00:00Added an answer on June 15, 2026 at 8:27 am

    Now I have discovered that I can answer my own questions 🙂 (silly me). So yeah, the problem was fixed a long time ago but to anyone who might be reading this, the solution was to change the line:

    if (body.position.x < (winSize.width - padding))
    

    To:

    if (body.position.x > (winSize.width - padding))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help finishing up this program, everything works and runs like I want
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help with crypto routines in Java. Given a PKCS#7 signature, I want
I need some help writing a MySQL trigger. what i want to do is
Need some help: I want to only pull back the results for a specific
Need some help with this error. Fresh wordpress 2.9 install... Fatal error: Cannot instantiate
Need some help on understanding how to do this; I'm going to be running
Need some help joining these two tables I have two views that looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.