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 7419803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:08:00+00:00 2026-05-29T08:08:00+00:00

I am working on an OpenGL ES application where I have a spaceship with

  • 0

I am working on an OpenGL ES application where I have a spaceship with 6 guns. Each gun uses keyframe animation to interpolate between 2 sets of vertices of a start and end position.

I have a method rotateGun: that I pass the gunNumber variable to that refers to which gun should fire. When rotateGun: fires, it creates a laser blast that I move away from the ship through a vector pointing down the gun barrel at it’s location when the method is called. This all works fine but I want to add random time intervals for each gun to fire since right now they appear to fire at the same time.

I’ve tried to create a time delay and fire off my rotateGun: method using performSelector:afterDelay:, but this doesn’t work. I then attempted to use the method mainGunFire: after a delay and then call rotateGun: on the main thread… this also didn’t work. By “didn’t work” I mean that the NSLog I inserted just before my drawing calls within the rotateGun: method does print, but the guns and blasts are never drawn.

If I simply do a performSelectorOnMainThread to call rotateGun:, then the guns and blasts are drawn as before and the blasts appear to fire at the same time. I’m clearly not understanding something. Can someone please help me understand how to slightly randomize my laser blasts so they don’t all fire at the same time? Thanks!

// Randomly Fire Gun
- (void)mainGunFire:(NSNumber *)gunNumber {

    // Perform the Gun animation and fire on main thread
    [self performSelectorOnMainThread:@selector(rotateGun:) withObject:gunNumber waitUntilDone:YES];

}

// Draw and Rotate the Guns
- (void)drawRotateGuns {

    // Only shoot if this is not a ship life
    if ( self.isLife == NO ) {

        // Gun 1

        // Set the pass variable to 1 and call the method
        // after a variable amount of time
        int randomTime = 0;
        //[self performSelector:@selector(mainGunFire:) withObject:[NSNumber numberWithInt:1] afterDelay:randomTime];
        [self performSelectorOnMainThread:@selector(rotateGun:) withObject:[NSNumber numberWithInt:1] waitUntilDone:YES];

        // Gun 2 ...

        // Gun 3 ...

    }
}
  • 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-05-29T08:08:00+00:00Added an answer on May 29, 2026 at 8:08 am

    The easiest solution is to use !(rand()%some_number) instead of 1. Experiment with some_number value (it must be different for each gun). But it should be not very big.

    For example if you use 2, then probability of !(rand()%2) == 1 is about 0.5. So if you’re rendering 60 frames per second you’ll get about 30 fires per second. For the !(rand()%20) you should get about 3 fires per second. Hope you get it. Some pseudocode:

    if( !(rand()%2) ) {
         [gun1 fire];
    }
    
    if( !(rand()%3) ) {
         [gun2 fire];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with sprite animation (OpenGL + C++). I have some trouble working with
I have this problem that when there is an OpenGL application I am working
I'm trying to get a simple multi-threaded OpenGL application working. I have an update
I'm working in OpenGL, with VBOs, and I have a set of three lines
I'm working on an android game with OpenGL, and I have a game update
My OpenGL application which was working fine on ATI card stopped working when I
I'm working on an application using OpenGL and C++ that parses some structured input
I have a somewhat complex data model in my iPad application (an OpenGL drawing
I m working on OpenGL tester program for Android. So i have pretty havy
I am working on an OpenGL application on my laptop. My app shows lots

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.