1

我和我的朋友被告知 Stackoverflow 是询问代码问题的地方。:)

我们对 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
4

1 回答 1

2

好像你的蚂蚁是你的 createAnt 方法的本地。所以在 touchesBegan 语句中,它没有“看到”蚂蚁。转到您的头文件,并在@interface 中声明蚂蚁...

CCSprite *蚂蚁;

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

ant=[CCSprite spriteWithSpriteFrameName:@"ant1.png"];

现在实现( .m )文件中的任何其他方法都会知道您在编写“ant”时的意思

希望能帮助到你!

于 2012-06-11T02:01:14.940 回答