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

  • Home
  • SEARCH
  • 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 8358505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:48:51+00:00 2026-06-09T10:48:51+00:00

I’m doing a simple game which user can throw one of the bottles against

  • 0

I’m doing a simple game which user can throw one of the bottles against the status bar on the top and break it. I’m using touchesMoved to test if a user catch a bottle(UIView) and it will be thrown responding to the user’s finger flicking direction. For the throwing part, it works, but I want it to be broken and changed to a broken bottle png if it throws out of frame, this part gives it headache. Here is part of the codes:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.view];

throwView = [self.view hitTest:location withEvent:nil];

//if the movement not on self.view, it's on the bottle.
if (bottleView != self.view) {
CGPoint loc = [aTouch locationInView:self.bottleView];
CGPoint prevloc = [aTouch previousLocationInView:self.bottleView];

//getting the finger moment
myFrame = self.bottleView.frame;
deltaX = loc.x - prevloc.x;
deltaY = loc.y - prevloc.y;

myLocation = bottleView.center;


//Here I try to multiply the finger movement to throw the bottles
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

myFrame.origin.x += deltaX*7.0;
myFrame.origin.y += deltaY*7.0; 
[self.bottleView setFrame:myFrame];
[bottleView setNeedsDisplay];
[UIView commitAnimations];


//if it hits the top of the frame..
    if (myFrame.origin.y <=20){
        [self hitTheTop];
    }
}}


   //this part gives me headache, I repeat the code again otherwise the bottle is broken before hitting the top.

-(void) hitTheTop{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

myFrame.origin.x += deltaX*7.0;
myFrame.origin.y += deltaY*7.0; 
[self.bottleView setFrame:myFrame];
[bottleView setNeedsDisplay];
[UIView commitAnimations];

//I calculate the slope and find where x should be when y is 0, 40 is half width of the bottle
myFrame.origin.x = myLocation.x + (myLocation.y / (-deltaY / deltaX)) - 40;
myFrame.origin.y = 0;

UIImageView *bottleBroken=
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"broken.png"]];
[bottleBroken setFrame:CGRectMake(-20, -30, 130, 180)];
[self.bottleView setFrame:myFrame];
[bottleView addSubview:bottleBroken];   
}

For now, what i get is first the bottle flies out of frame, for a very short while, it appears back on the edge of the frame and is already broken, it really looks unnatural. Actually I’m thinking to put it a timer to display the throw and it may manage better. But I just want to explore if there is simpler way to do it without the timer, thx for any suggestion!!

  • 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-09T10:48:52+00:00Added an answer on June 9, 2026 at 10:48 am

    Here’s how I do collision detection in my game.

    NSMutableArray *collidees = [NSMutableArray arrayWithArray:[[GRSceneController sharedSceneController] sceneObjects]]; 
    
    [collidees removeObject:self];
    
    NSMutableArray *collisions = [[NSMutableArray alloc] init];
    
    position.y += speed.y;
    
    for (GRBlock *obj in collidees) {
        if ([self.collider doesCollideWithCollider:obj.collider])
            [collisions addObject:obj];
    }
    
    if ([collisions count]) {
        [(GRSceneObject<GRColliderDelegate> *)self didCollideWith:collisions collisionType:kVerticalCollision];
    }
    
    [collisions removeAllObjects];
    

    Then I just repeat the same thing for position.x

    the doesCollideWithCollider: method looks like this

    -(BOOL)doesCollideWithCollider:(GRColliderObject *)aCollider {
    
        #if defined DEBUG_COLLIDER && defined LOG
        logRect(hitBox);
        logRect(aCollider.hitBox);
        #endif
    
        CGRect colliderBox = aCollider.hitBox;
    
        return CGRectIntersectsRect(hitBox, colliderBox);
    }
    

    hitBox is just a box that surrounds the object and acts as a collider, and didCollideWith:collisionType: handles the collision.

    The main thing to take away from this is that you should have a variable for the speed and to position.y += speed.y to create some sort of velocity. And check for a vertical collision after updating position.y and a horizontal collision after updating position.x

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words

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.