Drag a sprite in Cocos2d

This tutorial shows you how to touch and drag an image across the Iphone screen.

Start a new project.

Add the following to HelloWorldScene.h

@interface HelloWorld : CCLayer
{
CCSprite *_MoveableSprite;
bool _MoveableSprite1touch;
}

Replace the init method in the HelloWorldScene.m

-(id) init{
if( (self=[super init] )) {
_MoveableSprite1touch=FALSE;
self.isTouchEnabled = YES;
_MoveableSprite =[CCSprite spriteWithFile:@"Eyesopened.jpg"];
_MoveableSprite.position = ccp(_MoveableSprite.contentSize.width/2, _MoveableSprite.contentSize.height/2);
[self addChild:_MoveableSprite];
}
return self;
}

I have covered all this code in my previous post if you don’t understand it.

Now to the good stuff, add the following below your init method.

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
CGRect MoveableSpriteRect = CGRectMake(_MoveableSprite.position.x(_MoveableSprite.contentSize.width/2),
_MoveableSprite.position.y(_MoveableSprite.contentSize.height/2),
_MoveableSprite.contentSize.width,
_MoveableSprite.contentSize.height);

if (CGRectContainsPoint(MoveableSpriteRect, location)) {
_MoveableSprite1touch=TRUE;
}
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point];
if(_MoveableSprite1touch==TRUE){
[_MoveableSprite setPosition:point];
}
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_MoveableSprite1touch=FALSE;
}

This code checks to see if when you begin a touch the touch intersects the _MoveableSprite. If it does not then nothing happens, but if it does _MoveableSprite1touch equal to true. If you slide your finger when _MoveableSprite1touch equals true then the sprite will move with your finger. When you pick up you finger _MoveableSprite1touch is set to false. The program will go thru this process with each touch.

NEW STUFF
Ok so I saw a question on the cocos2d iphone form about keeping a movable sprite from going off the screen so the code for that is below.

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        CGSize size = [[CCDirector sharedDirector] winSize];
    UITouch *myTouch = [touches anyObject];
        CGPoint point = [myTouch locationInView:[myTouch view]];
        point = [[CCDirector sharedDirector] convertToGL:point];
        //CCNode *sprite = [self getChildByTag:kTagBall];
        if(_MoveableSprite1touch==TRUE){
//This code prevents the sprite from moving off the screen.
        int width=point.x;
        int height=point.y;
        if (size.width-_MoveableSprite.contentSize.width/2<width) {
            width=size.width-_MoveableSprite.contentSize.width/2;
        }
        if (size.height-_MoveableSprite.contentSize.height/2<height) {
            height=size.height-_MoveableSprite.contentSize.height/2;
        }
        if (_MoveableSprite.contentSize.height/2>height) {
            height=_MoveableSprite.contentSize.height/2;
        }
        if (_MoveableSprite.contentSize.width/2>width) {
            width=_MoveableSprite.contentSize.width/2;
        }
                [_MoveableSprite setPosition:ccp(width,height)];
        }
        //return YES;
}
 

Download the code for drag a sprite in Cocos2d

Copy right Britney Lee Johnson August 2010.

This entry was posted in cocos2d. Bookmark the permalink.

7 Responses to Drag a sprite in Cocos2d

  1. Its such as you learn my mind! You appear to know so much about this, such as you
    wrote the e-book in it or something. I believe that you just could do with some percent to force the message house a
    bit, but instead of that, this is great blog. A great read.
    I will definitely be back.

  2. Broderick says:

    Hey would you mind stating which blog platform you’re
    using? I’m planning to start my own blog soon but I’m having a hard time choosing
    between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m looking for something unique.
    P.S My apologies for getting off-topic but I had to ask!

    My page kids going to the dentist (Broderick)

  3. Hello, i feel that i noticed you visited my weblog so i
    got here to return the want?.I am attempting
    to find issues to enhance my site!I guess its good enough
    to use a few of your ideas!!

    Here is my web page – hire moving companies (gpfiladelfia.org)

  4. Thanks very interesting blog!

    Look at my weblog

  5. Very good write-up. I absolutely love this site. Keep it up!

  6. Carmella says:

    You are so cool! I do not suppose I’ve read something like that before.
    So good to find someone with a few genuine thoughts on this subject.

    Seriously.. many thanks for starting this up. This site is something that is needed on the internet, someone with some originality!

    Look at my blog: website (Carmella)

  7. Samara says:

    Writing articles is pain in the ass.I know where you can get unlimited
    articles for your page, type in google:
    Anightund’s rewriter

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>