2012-06-10 98 views
1

我和我的朋友被告知Stackoverflow是提问有关代码的问题的地方。 :)Cocos2d,将精灵移动到触摸位置

我们对Objective-C,Xcode和Cocos2d相当陌生,并且正在尝试完成简单的任务以提高我们的知识水平。

看看我们下面的代码,我们制作了一个Ant对象并将其放置在屏幕上。我们现在想要使用触摸位置将蚂蚁移动到用户触摸的位置。

我们不确定正确的方法是什么。目前我们远离创建类,因此将所有代码放在一个地方,但我们无法弄清楚如何让屏幕上的蚂蚁进入触摸位置。

任何人都可以帮忙吗?

// 
// HelloWorldLayer.m 
// Timer 
// 
// Created by Lion User on 06/06/2012. 
// Copyright __MyCompanyName__ 2012. All rights reserved. 
// 


// Import the interfaces 
#import "HelloWorldLayer.h" 

// HelloWorldLayer implementation 
@implementation HelloWorldLayer 

+(CCScene *) scene 
{ 
    // 'scene' is an autorelease object. 
    CCScene *scene = [CCScene node]; 

    // 'layer' is an autorelease object. 
    HelloWorldLayer *layer = [HelloWorldLayer node]; 

    // add layer as a child to scene 
    [scene addChild: layer]; 

    // return the scene 
    return scene; 
} 



// on "init" you need to initialize your instance 
-(id) init 
{ 
    if((self=[super initWithColor:ccc4(225, 225, 225, 255)])) { 

     // enable touches 
     self.isTouchEnabled=YES; 

     // ask director the the window size 
     CGSize size = [[CCDirector sharedDirector] winSize]; 


     // set time to zero 
     myTime = currentTime; 
     timeLabel = [CCLabelTTF labelWithString:@"00:00" fontName:@"Arial" fontSize:48]; 
     timeLabel.position = CGPointMake(size.width/2, size.height); 
     // set label color 
     timeLabel.color = ccBLACK; 
     // Adjust the label's anchorPoint's y position to make it align with the top. 
     timeLabel.anchorPoint = CGPointMake(0.5f, 1.0f); 
     // Add the time label 
     [self addChild:timeLabel]; 

     // run create ant method 
     [self createAnt]; 

     //update 
     [self schedule:@selector(update:)]; 

    } 
    return self; 
} 

-(void)update:(ccTime)dt{ 

    totalTime += dt; 
    currentTime = (int)totalTime; 
    if (myTime < currentTime) 
    { 
     myTime = currentTime; 
     [timeLabel setString:[NSString stringWithFormat:@"%02d:%02d", myTime/60, myTime%60]]; 
    } 

} 

////* method for creating an ant and moving it*//// 
-(void)createAnt{ 

    ////*requirements for animation setup*//// 

    // create cache object to store spritesheet in 
    CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache]; 
    // add the sprite list to the cache object 
    [cache addSpriteFramesWithFile:@"antatlas.plist"]; 
    // create frame array to store the frames in 
    NSMutableArray *framesArray=[NSMutableArray array]; 

    //loop through each frame 
    for (int i=1; i<3; i++){ 
     // increment the name to include all frames in sprite sheet 
     NSString *frameName=[NSString stringWithFormat:@"ant%d.png", i]; 
     // create frame object set it to the cache object and add the frameNames to the cache 
     id frameObject=[cache spriteFrameByName:frameName]; 
     // add the frame object into the array 
     [framesArray addObject:frameObject]; 

    } 

    ////* setup the actions for running the animation*//// 

    // create animation object and pass the list of frames to it (it expects this as an array) 
    id animObject=[CCAnimation animationWithFrames:framesArray delay:0.05]; 
    // setup action to run the animation do not return to frame 1 
    id animationAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO]; 
    //loop the animation indefinitely 
    animationAction = [CCRepeatForever actionWithAction:animationAction]; 
    // move ant action 
    id moveAnt=[CCMoveTo actionWithDuration:3 position:ccp(60, 160)]; 

    // create sprite, set location and add to layer (starts with the name of the first frame in the animation 
    CCSprite *ant=[CCSprite spriteWithSpriteFrameName:@"ant1.png"]; 
    ant.position=ccp(240, 160); 
    [self addChild:ant]; 


    //run animation action 
    [ant runAction: animationAction]; 
    // run move ant action 
    [ant runAction:moveAnt]; 

} 

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch=[touches anyObject]; 
    CGPoint loc=[touch locationInView:[touch view]]; 
    loc=[[CCDirector sharedDirector]convertToGL:loc]; 
    NSLog(@"touch (%g,%g)",loc.x,loc.y); 

    // Move ant to point that was pressed 
    [ant runAction:[CCSequence actions: 
    [CCMoveTo actionWithDuration:realMoveDuration position:loc], 
    [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)], 
    nil]]; 
} 


// on "dealloc" you need to release all your retained objects 
- (void) dealloc 
{ 

    [super dealloc]; 
} 
@end 

回答

2

看起来像你的蚂蚁是你的createAnt方法的本地。所以在touchesBegan声明中,它不会“看到”蚂蚁。转到您的头文件,并在@interface中声明蚂蚁...

CCSprite * ant;

然后回到你的createAnt语句,你可以只写...

蚂蚁= [CCSprite spriteWithSpriteFrameName:@ “ant1.png”];

现在在执行(.M)文件中的任何其他方法会知道你的意思,当你写的“蚁族”

希望它能帮助!

+0

我认为有效的答案。 +1 –