Hi
I've managed to add touch actions to my layers but how do I limit them to only the area of my sprites?
Thanks
-alex
limiting touch to sprites rather than whole layer
(10 posts) (4 voices)-
Posted 2 years ago #
-
You should check if the touch point is inside the sprite area, easiest thing I guess would be to implement a
-(CGRect)getRect;method inside your sprite class. Just make sure you have a rect for the sprite...- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint loc = [touch locationInView:[touch view]]; if(CGRectContainsPoint([mySprite getRect], loc)) { //Sprite got touched... } }There's possibly a way to create a sprite class that will register touches and cancel the touch registration for the layer but I'm not sure whether it will work or not.
Posted 2 years ago # -
Do I have to add the location of my sprite and in what format if so? Could I get an example?
Posted 2 years ago # -
anybody?
Posted 2 years ago # -
Do you use the
TouchDispatcher?Posted 2 years ago # -
nope
Posted 2 years ago # -
Well, maybe you should cause it does exactly what you want. Look at the TouchesTest example that comes with cocos (see Paddle.m which has an example of how to use the
TouchDispatcher).Posted 2 years ago # -
I've been looking at TouchesTest and coming into Cocos2D only a month ago, I can't tell the difference between TouchDispatcher and the old way of handling touches.
Posted 2 years ago # -
old :
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
new :- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)eventold : you get an
NSSetof a certain number of touches
new : you only get one touchold : called at the
Layerlevel ifisTouchEnabledis set toYES
new : called at theSprite(TextureNodein fact) level if you registered to theTouchDispatcherPosted 2 years ago # -
hmmm. [digesting...]
Posted 2 years ago #
Reply
You must log in to post.