2012-06-10 100 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 –