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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:00:10+00:00 2026-06-15T10:00:10+00:00

It is so easy to write some code to achieve the bouncing ball based

  • 0

It is so easy to write some code to achieve the bouncing ball based on the open source project cocos2d,here is the question.

So without cocos2d, how to implement this with the simplest code.

  • 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-15T10:00:12+00:00Added an answer on June 15, 2026 at 10:00 am

    First you need to create a ball class. This class should have ivars for position, velocity, direction etc.
    It should also have some methods for when the ball needs to change direction, like hitTop, hitBottom, hitLeft, hitRight. It should also have a method for updating the position, to move it one step forward.
    I also have a BallView so that I can use a image or just a color as the ball.

    To make it move you need to have a gameloop in you view controller, there are many methods for doing that but the one I find easiest is CADisplayLink.

    Here are some code if you have problems implementing it:

    Ball.h

    #import <Foundation/Foundation.h>
    #import "MovingObjectView.h"
    
    @interface MovingObject : NSObject
    
    @property(nonatomic, retain) MovingObjectView *ball;
    
    @property CGRect bounds;
    @property CGPoint position;
    @property CGPoint direction;
    @property float velocity;
    
    -(id)initWithBounds:(CGRect)b andPosition:(CGPoint)p;
    
    -(BOOL)positionIsInsideRect;
    -(void)update;
    
    -(void)hitLeft;
    -(void)hitRight;
    -(void)hitTop;
    -(void)hitBottom;
    
    @end
    

    Ball.m

    #import "MovingObject.h"
    
    @implementation MovingObject
    
    -(id)initWithBounds:(CGRect)b andPosition:(CGPoint)p
    {
        if (self = [super init]) {
            self.bounds = b;
            self.position = p;
            self.direction = CGPointMake(1, 1);
    
            //Change the position to middle if position is outside bounds
            if (![self positionIsInsideRect]) {
                NSLog(@"Position changed to center");
                self.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
            }
    
            self.ball = [[[MovingObjectView alloc]initWithFrame:CGRectMake(self.position.x, self.position.y, 15, 15)]autorelease];
        }
        return self;
    }
    
    //checks if the balls position is correct
    -(BOOL)positionIsInsideRect {
        if (self.position.x < self.bounds.origin.x || self.position.x > self.bounds.size.width || self.position.y < self.bounds.origin.y || self.position.y > self.bounds.size.height) {
            NSLog(@"Position is outside bounds");
            return NO;
        }else{
            return YES;
        }
    }
    
    //Call this method to move a ball
    -(void)update {
        //Checks if the ball is outside bounds
        if (self.position.x-(self.ball.frame.size.width/2) <= self.bounds.origin.x) {
            [self hitLeft];
        }else if (self.position.x+(self.ball.frame.size.width/2) >= self.bounds.size.width){
            [self hitRight];
        }else if (self.position.y-(self.ball.frame.size.height/2) <= self.bounds.origin.y) {
            [self hitTop];
        }else if (self.position.y+(self.ball.frame.size.height/2) >= self.bounds.size.height){
            [self hitBottom];
        }
    
        //Updates the balls position
        CGPoint p = self.position;
        p.x += self.direction.x*self.velocity;
        p.y += self.direction.y*self.velocity;
        self.position = p;
    
        self.ball.center = self.position;
    }
    
    //Call this when the ball need to bounce
    -(void)hitLeft
    {
        NSLog(@"hitLeft");
        CGPoint d = self.direction;
        d.x = fabsf(self.direction.x);
        self.direction = d;
    }
    
    -(void)hitRight
    {
        NSLog(@"hitRight");
        CGPoint d = self.direction;
        d.x = -fabsf(self.direction.x);
        self.direction = d;
    }
    
    -(void)hitTop
    {
        NSLog(@"hitTop");
        CGPoint d = self.direction;
        d.y = fabsf(self.direction.y);
        self.direction = d;
    }
    
    -(void)hitBottom
    {
        NSLog(@"hitBottom");
        CGPoint d = self.direction;
        d.y = -fabsf(self.direction.y);
        self.direction = d;
    }
    
    -(void)dealloc {
        [super dealloc];
    }
    
    @end
    

    BallView.h

    #import <UIKit/UIKit.h>
    
    @interface MovingObjectView : UIView
    
    @property (nonatomic, retain) UIImage *img;
    
    @end
    

    BallView.m

    #import "MovingObjectView.h"
    
    @implementation MovingObjectView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        if (self.img == nil) {
            CGRect bounds = [self bounds];
            [[UIColor blackColor]set];
            UIRectFill(bounds);
        }else {
            CGRect bounds = [self bounds];
            [[UIColor whiteColor]set];
            UIRectFill(bounds);
            [self.img drawInRect:rect];
        }
    }
    
    @end
    

    Then finally in the viewcontroller you need this:

    ViewController

    In viewdidload:

    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    

    Then add this method:

    -(void)gameLoop
    {   
        //Updates the balls position
        [self.movingBall update];
    
        //Here you could add your collision detection code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some simple Rcpp code examples. This is remarkably easy
Is there an easy way to write C code that can access its Git
Some easy points for somebody here, take the following table of data: EventCode ProcessId
I need to write some code that deals with generating and manipulating multivariable polynomials.
In another question, a user suggested to write code like to that: def list
I am trying to write some code that allows SVG elements to be dragged
Hello i am searching free api or some easy code to encrypt and decrypt
Here are some (overly) simplified code example to describe my unit testing method. CompanyDataSet.xml
I need to write some code to convert an array of quads into a
I am refactoring some code that I did not write, and I found a

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.