开发者

CCTouchesBegan - CCTouchesEnded need both coordinates

first of all i am new to Cocos2D and Obj-C so i do encounter maybe simple problems like this. my problem looks like this: there is a sprite on the screen and the user will have to touch the top of it and while still pressed move a bit upwards and then release the touch. imagine a player sprite with a hat, where you have to touch the hat and move your finger a bit towards to top to make his hat fly into that direction. whats the best way to realize this?

what i got so far is this:

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

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
        //other stuff
}

do i need to create another method to determine the CCTouchesBegan location and then give that values to the CCTouchesEnded method, where i then calculate the angle and make the hat fly away? or can i determine the position where the touch began in the above method itself?

Thank you very much for a开发者_开发百科ny answer :)


That method you have posted only covers when the touch ends. This means that it will only trigger when the user takes their finger off the screen. If that is what you want then that is fine. The CGPoint variable "location" is your point that you want.

A CGPoint is two float values. You will have:

location.x
location.y

This will not tell you where the touch began. As I said, it only triggers when the user takes their finger off the screen. If you need to know where the user touched the screen there is another method for that. Here are the standard touches methods:

// Touch first detected
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
// Touches moved
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
// User took finger off screen
- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
// Touch was somehow interrupted
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event


UITouch *touch = [ touches anyObject];

CGPoint new_location = [touch locationInView: [touch view]];
new_location = [[CCDirector sharedDirector] convertToGL:new_location];
NSLog(@"y: %1f", new_location.x);
NSLog(@"x: %1f", new_location.y);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜