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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:07:35+00:00 2026-05-13T09:07:35+00:00

i have 10 moving objects (UIImageView), is there a better way to write this

  • 0

i have 10 moving objects (UIImageView),
is there a better way to write this code?

    - (void) jumpOnTimer {

        jumpBall1.center = CGPointMake(jumpBall1.center.x+pos1.x,jumpBall1.center.y+pos1.y);

        if(jumpBall1.center.x > 60 || jumpBall1.center.x < 0)
            pos1.x = -pos1.x;
        if(jumpBall1.center.y > 211 || jumpBall1.center.y < 82)
            pos1.y = -pos1.y;

        jumpBall2.center = CGPointMake(jumpBall2.center.x+pos2.x,jumpBall2.center.y+pos2.y);

        if(jumpBall2.center.x > 40 || jumpBall2.center.x < 0)
            pos2.x = -pos2.x;
        if(jumpBall2.center.y > 206 || jumpBall2.center.y < 82)
            pos2.y = -pos2.y;

and so on...
  • 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-13T09:07:36+00:00Added an answer on May 13, 2026 at 9:07 am

    Judging by that code snippet, it looks like you have a single controller which “owns” the ten balls, and you want the balls to bounce around according to a set of rules that are unique to each ball. A more object-oriented approach would be as follows:

    @interface JumpBallClass
    {
        CGPoint center;
        CGPoint speed;
    
        CGPoint lowerLimit;
        CGPoint upperLimit;
    }
    @property (assign) CGPoint lowerLimit;
    @property (assign) CGPoint upperLimit;
    - (void)update;
    @end
    
    @implementation JumpBallClass
    - (void)update
    {
        center.x += speed.x;
        center.y += speed.y;
    
        if (center.x > upperLimit.x || center.x < lowerLimit.x)
        { speed.x = -speed.x; }
        if (center.y > upperLimit.y || center.y < lowerLimit.y)
        { speed.y = -speed.y; }
    }
    @end
    

    This setup would allow you to configure all of the balls once, by setting their upper and lower limits:

    [jumpBall1 setUpperLimit:CGPointMake(60, 211)];
    [jumpBall1 setLowerLimit:CGPointMake(0, 82)];
    ...
    

    And then simply calling update on each ball in your timer method:

    - (void) jumpOnTimer {          
    
        [jumpBall1 update];          
        [jumpBall2 update];
        ...
    }
    

    You can simplify this even further by storing all of the balls in an NSArray:

    NSArray * balls = [NSArray arrayWithObjects:jumpBall1, jumpBall2, ..., nil];
    

    And then calling makeObjectsPerformSelector:

    [balls makeObjectsPerformSelector:@selector(update)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Panel in which some 2D objects are moving about. I have
I am making a 2d space game with many moving objects. I have already
I have the functions for moving objects around in a figure. The figure is
I have one triangle ad one circle moving objects on plane.How will i know
I came across this interview question Many irregularly shaped objects are moving in random
I have around several hundreds of moving objects within screen at the given time,
I have a three points (A,B,C) that denote objects moving in 2D space. For
I have a issue finding the center of the screen. I have jQuery code:
I have multiple objects moving about in a 3D space and am looking for
I have a rectangle on canvas. I can already moving this object using with

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.