1

私と私の友人は、Stackoverflow はコードに関する質問をする場所だと言われました。:)

私たちは、Objective-C、Xcode、および Cocos2d にかなり慣れていないため、知識を深めるために簡単なタスクを完了しようとしています。

以下のコードを見ると、Ant オブジェクトが作成され、Ant オブジェクトが画面に配置されています。ここで、タッチ位置を使用して、ユーザーがタッチした位置にアリを移動させたいと考えています。

それを行う正しい方法が何であるかはわかりません。現時点では、クラスの作成から離れているため、すべてのコードを 1 か所にまとめていますが、画面上のアリをタッチ位置に移動させる方法がわかりません。

誰でも助けることができますか?

    //
//  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 で ant を宣言します...

CCSprite *ant;

次に、createAnt ステートメントに戻って、次のように書くことができます...

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

これで、実装 ( .m ) ファイル内の他のメソッドは、「ant」と書いたときの意味がわかります。

それが役に立てば幸い!

于 2012-06-11T02:01:14.940 に答える