Reputation: 33
Hi Im trying to make a simple game using the accelerometer on the iphone.
So I have a UIView subclass called enemyBlock. and I have a couple of these laid around on the screen. And these blocks expand and contract using [UIView animateWithDuration:...] over and over. I have an UIImageView that moves around according to the accelerometer data - this is the player.
When ever the player comes in contact with an enemyBlock the game should reset. Im using CGRectIntersectsRect(player.frame, enemyblock.frame) to detect if a hit has occurred. But this behaves very strangely as a hit occurs only when the enemyblocks are not animating otherwise the player can pass right through them.
Any clue as to why this happens?
Upvotes: 2
Views: 493
Reputation: 2635
When animating views, its the presentation layer of the view that is updated with the geometry information during the animation. Depending on how you are animating your player layer try
CGRectIntersectsRect(player.frame, enemyblock.layer.presentationLayer.frame)
All the best
Upvotes: 1
Reputation: 1070
Not quite an answer but I would advise using cocos2d if you are writing a game. It's an awesome game engine written in Objective C that I've used myself. You create a 2D scenegraph of sprites. You then write an update method which gets called at regular intervals, e.g. 30 times per second. In this update method you can update the position for each enemy and player sprite incrementally and do collision checking using CGRectIntersectsRect to detect collisions. Hope this helps.
Upvotes: 1
Reputation: 7789
CGRect does not detect During the animation of UIView and UIImageView therefore "CGRectIntersectsRect(player.frame, enemyblock.frame) " doesn't play appropriate role and your faced something strangely. My advise to you make custom animation of UIView it will work for you.
Upvotes: 0