PDA

View Full Version : CGRectIntersectsRect problem, need help pls




newbie80
Apr 12, 2011, 07:35 PM
Hi,

I have a problem with my CGRectIntersectsRect when my "player" intersects with my "word3" is not working. But my CGRectIntersectsRect for "word1" and "word2" are working fine. I don't know why, hope anyone can help, thanks. My program works like this, left arrow key to move the object to left, right key to move the object to right, up arrow key to make the object jump. And word1, word2 and word3 will be moving from right to left on top of the object. So the object needs to jump in order to hit the words.

header file:

#import <UIKit/UIKit.h>

@interface MainView : UIView {
IBOutlet UIView *main_view;
IBOutlet UIView *second_view;

IBOutlet UIImageView *player;

IBOutlet UIImageView *word1;
IBOutlet UIImageView *word2;
IBOutlet UIImageView *word3;

NSTimer *leftTimer;
NSTimer *leftTimer2;
NSTimer *leftTimer3;
}

-(IBAction)switchViews;
-(IBAction)backToMain;

-(IBAction)jumpPress;
-(IBAction)leftPress;
-(IBAction)rightPress;

-(void)jumpStance;
-(void)cleanStance;
-(void)goLeft;
-(void)goLeft2;
-(void)goLeft3;

-(void)checkcollision;
-(void)checkWordLeft;

-(void)initgame;

@property (nonatomic, retain) UIImageView *player;

@property(nonatomic, retain) UIImageView *word1;
@property(nonatomic, retain) UIImageView *word2;
@property(nonatomic, retain) UIImageView *word3;

@end



main file:


#import "MainView.h"

@implementation MainView

@synthesize player;
@synthesize word1, word2, word3;

int soundCounter = 0;

/********************** SWITCH VIEW **********************/
- (IBAction)switchViews {
[UIView beginAnimations:Nil context:NULL];
[UIView setAnimationDelay:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
[main_view removeFromSuperview];
[self addSubview:second_view];
[UIView commitAnimations];

//initial position word1
CGPoint newCenter1 = CGPointMake(-50, -50);
word1.center = newCenter1;
[word1 removeFromSuperview];

//initial position word2
CGPoint newCenter2 = CGPointMake(-50, -50);
word2.center = newCenter2;
[word2 removeFromSuperview];

//initial position word3
CGPoint newCenter3 = CGPointMake(-50, -50);
word3.center = newCenter3;
[word3 removeFromSuperview];

[self initgame];

[leftTimer invalidate];
leftTimer = nil;
[leftTimer2 invalidate];
leftTimer2 = nil;
[leftTimer3 invalidate];
leftTimer3 = nil;

}

/********************** BACK TO MAIN **********************/
- (IBAction)backToMain{
[UIView beginAnimations:Nil context:NULL];
[UIView setAnimationDelay:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
[second_view removeFromSuperview];
[self addSubview:main_view];
[UIView commitAnimations];

[word1 removeFromSuperview];
[word2 removeFromSuperview];
[word3 removeFromSuperview];


}

/********************** INITGAME ***************************/
-(void)initgame{
//timer for adding word to view
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(addWord:) userInfo:nil repeats:NO];

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(addWord2:) userInfo:nil repeats:NO];

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(addWord3:) userInfo:nil repeats:NO];
}

/********************** NORMAL STANCE **********************/
- (void)normalStance {
[self.player setImage:[UIImage imageNamed:@"monkey_pix.png"]];
}

/********************** CLEAN STANCE **********************/
- (void)cleanStance {
[self.player setImage:nil];
self.player.animationImages = nil;
}

/********************** JUMP STANCE **********************/
-(void)jumpStance {
[self cleanStance];
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"monkey_pix.png"],
[UIImage imageNamed:@"monkey_pix.png"],
nil];
self.player.animationImages = imageArray;
self.player.animationDuration = 0.3;

[self.player startAnimating];
}

/********************** FALL **********************/
-(void)fall:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[self cleanStance];
[self normalStance];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.2];
self.player.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
}

/********************** JUMP **********************/
- (void)jump:(UIImageView *)image {
[self jumpStance];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.3];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
// execute fall after the animation ended
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fall:finished:context:)];
image.transform = CGAffineTransformMakeTranslation(0, -240);
[UIView commitAnimations];
}

/********************** JUMP PRESS **********************/
- (IBAction)jumpPress {
[self jump:self.player];
[self checkcollision];
}

/********************** CHECK COLLISION **********************/
- (void)checkcollision {
if (CGRectIntersectsRect(player.frame, word1.frame)) {
NSLog(@"hit1");

CGPoint newCenter1 = CGPointMake(-50, -50);
word1.center = newCenter1;
[word1 removeFromSuperview];

[leftTimer invalidate];
leftTimer = nil;
}
if (CGRectIntersectsRect(player.frame, word2.frame)) {
NSLog(@"hit2");

CGPoint newCenter2 = CGPointMake(-50, -50);
word2.center = newCenter2;
[word2 removeFromSuperview];

[leftTimer2 invalidate];
leftTimer2 = nil;
}
if (CGRectIntersectsRect(player.frame, word3.frame)) {
NSLog(@"hit3");

CGPoint newCenter3 = CGPointMake(-50, -50);
word3.center = newCenter3;
[word3 removeFromSuperview];

[leftTimer3 invalidate];
leftTimer3 = nil;
}
}

/********************** LEFT PRESS **********************/
- (IBAction)leftPress {
player.center = CGPointMake(player.center.x-15, player.center.y);
if(player.center.x <= 25){
CGPoint playerCenter = CGPointMake(25, player.center.y);
player.center = playerCenter;
}
}

/********************** WORD AUTO MOVE LEFT **********************/
- (void)autoLeft:(NSTimer *)timer {
leftTimer = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(goLeft) userInfo:nil repeats:YES];
if(leftTimer == nil){
leftTimer = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(goLeft) userInfo:nil repeats:YES];
}
}

- (void)autoLeft2:(NSTimer *)timer2 {
leftTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(goLeft2) userInfo:nil repeats:YES];
if(leftTimer2 == nil){
leftTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(goLeft2) userInfo:nil repeats:YES];
}
}

- (void)autoLeft3:(NSTimer *)timer3 {
leftTimer3 = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(goLeft3) userInfo:nil repeats:YES];
if(leftTimer3 == nil){
leftTimer3 = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(goLeft3) userInfo:nil repeats:YES];
}
}

/********************** GO LEFT **********************/
- (void)goLeft {
word1.center = CGPointMake(word1.center.x-5, word1.center.y);
[self checkWordLeft];
}
- (void)goLeft2 {
word2.center = CGPointMake(word2.center.x-5, word2.center.y);
[self checkWordLeft];

}
- (void)goLeft3 {
word3.center = CGPointMake(word3.center.x-5, word3.center.y);
[self checkWordLeft];

}


/********************** CHECK WORD LEFT **********************/
-(void)checkWordLeft {
if(word1.center.x <= 0){
CGPoint word1Center = CGPointMake(300, 100);
word1.center = word1Center;
}
if(word2.center.x <= 0){
CGPoint word2Center = CGPointMake(300, 150);
word2.center = word2Center;
}
if(word3.center.x <= 0){
CGPoint word3Center = CGPointMake(300, 200);
word3.center = word3Center;
}
}



/********************** RIGHT PRESS **********************/
- (IBAction)rightPress {
player.center = CGPointMake(player.center.x+15, player.center.y);

if(player.center.x >= 290){
CGPoint playerCenter = CGPointMake(290, player.center.y);
player.center = playerCenter;
}
}

/********************** ADD WORD TO VIEW **********************/
- (void)addWord:(NSTimer *)timer {
CGPoint newCenter1 = CGPointMake(300, 100);
word1.center = newCenter1;
[self addSubview:word1];

//timer for auto left
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(autoLeft:) userInfo:nil repeats:NO];
}

- (void)addWord2:(NSTimer *)timer2 {
CGPoint newCenter2 = CGPointMake(300, 150);
word2.center = newCenter2;
[self addSubview:word2];

//timer for auto left
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(autoLeft2:) userInfo:nil repeats:NO];
}

- (void)addWord3:(NSTimer *)timer3 {
CGPoint newCenter3 = CGPointMake(300, 200);
word3.center = newCenter3;
[self addSubview:word3];

//timer for auto left
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(autoLeft3:) userInfo:nil repeats:NO];
}

- (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
{
// Drawing code
}
*/

- (void)dealloc
{
[super dealloc];:(
}

@end



Under CHECK WORD LEFT, I tried to modified the value of following bold text to 150 then it works, but when I put the value 200, it's not working.

if(word3.center.x <= 0){
CGPoint word3Center = CGPointMake(300, 200);
word3.center = word3Center;
}