0 Replies - 910 Views - Last Post: 28 June 2014 - 10:37 AM Rate Topic: -----

#1 rodka99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-June 14

Sticking image to another image

Posted 28 June 2014 - 10:37 AM

I currently have some code which creates a UIImageView at a random location which is animated towards the bottom of the screen. When that object collides with another object which is controlled by the user, the collision is detected. However, I do not know how to then make the random image stay on top of of the user controlled image after the collision so that the random image moves together with the user controlled one. This is the code that I have so far:

-(void)spawnMechanism{
    timeSinceLastSpawn++;


    if(timeSinceLastSpawn >= 3)
    {
        if( arc4random() % 10 < 7 ){


            UIImageView *spawnGood;

            spawnGood=[[UIImageView alloc]initWithFrame:CGRectMake(arc4random() % 700, -100, 70,70)];
            UIImage *image;
            image=[UIImage imageNamed:@"friendly-01"];
            [spawnGood setImage:image];
            [self.view addSubview:spawnGood];

            NSLog(@"spawnGood spawned");
            [UIView animateWithDuration:2.0
                                  delay: 0.0
                                options: UIViewAnimationOptionCurveLinear
                             animations:^{
                                 CGRect frame1 =[spawnGood frame];

                                 frame1.origin.y =frame1.origin.y+950;
                                 [spawnGood setFrame:frame1];

                             }
                             completion:^(BOOL finished){

                                 if(CGRectIntersectsRect(userToken.frame, spawnGood.frame)){
                                     NSLog(@"Good Collision");

                                 }else{
                                     [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
                                         CGRect frame1 =[spawnGood frame];

                                         frame1.origin.y =frame1.origin.y+950;
                                         [spawnGood setFrame:frame1];
                                     }
                                                      completion:nil];

                                     };
                             }

             ];}









            }





        }

To control the user image, I have this code:

- (IBAction)right {
    /*[UIView animateWithDuration:0.10f
                     animations:^{
                         _userToken.frame = CGRectMake(_userToken.frame.origin.x + 80, _userToken.frame.origin.y, _userToken.frame.size.width, _userToken.frame.size.height);
                     }];
}*/
    if(goRight==nil){
    goRight=[NSTimer scheduledTimerWithTimeInterval:rate
                                            target:self
                                          selector:@selector(goRight)
                                          userInfo:nil
                                           repeats:YES];
    }

        [self myDetectCollisionsOnRight];
}

- (IBAction)left{
    /*[UIView animateWithDuration:0.10f
                     animations:^{
                         _userToken.frame = CGRectMake(_userToken.frame.origin.x - 80, _userToken.frame.origin.y, _userToken.frame.size.width, _userToken.frame.size.height);
                     }];
}*/
    if(goLeft==nil){
        goLeft=[NSTimer scheduledTimerWithTimeInterval:rate
                                           target:self
                                         selector:@selector(goLeft)
                                         userInfo:nil
                                          repeats:YES];
    }
        [self myDetectCollisions];
}

-(IBAction)stopLeft{
    [goLeft invalidate];
    goLeft=nil;
}


-(IBAction)stopRight{
    [goRight invalidate];
    goRight=nil;
}

-(void)goLeft{
    userToken.center =CGPointMake(userToken.center.x -25, userToken.center.y);
    [self myDetectCollisions];


}

-(void)goRight{
    userToken.center =CGPointMake(userToken.center.x +25, userToken.center.y);
    [self myDetectCollisionsOnRight];

}

Is anyone able to suggest a solution to this?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1