2009-06-04 13 views

答えて

3

実行するアクションを定義します。この場合は... ScaleBy(最終スケールではなく、現在のスケールに比例します)。タッチが受信されると、タッチが終了するまでこのアクションを繰り返して、アクションを停止します。

アイテムが特定のポイントを超えて拡大しないように上限を実装する場合、ScaleByはScaleToになり、上限はターゲットスケールとして設定されます。しかし、あなたが探している気分を得るためには、期間を周りに遊ばなければなりません。あまりにも速く、遅すぎる、など...それらはあなた次第です。

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch* touch = [touches anyObject]; 
    CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location]; 
    CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height); 
    if(CGRectContainsPoint(particularSpriteRect, location)) { 
    // scale by 1.25 every 0.25 second while finger touching 
    id a = [RepeatForever actionWithAction: [ScaleBy actionWithDuration: 0.25 scale: 1.25]; 
    [a setTag: YOUR_TAG]; 
    [particularSprite runAction: a]; 
    return kEventHandled; 
    } 
} 

- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 
    // code to determine correct finger/touch releases omitted 
    [particularSprite stopActionByTag: YOUR_TAG]; 
} 
0

まあ...スプライトクラスには、デフォルトの( "ネイティブ")スケールである浮動小数点値として、そのスケールを設定できるscaleメソッドがあるようです。

スプライトの縮尺を変えたい場合、タッチの「追従」、つまりドラッグすることによってスプライトのサイズを変更できるようになっている場合や、たとえば、タッチが登録されたことをユーザーに通知するために、スケールを変更します。あなたはこれらのことを理解し、あなたが望むスケールを計算する必要があります。

1

私はあなたが正しく動くことなく同じ場所に指が残っている間にスプライトをスケールしたいのですか?

私は次の方法でLayerを使用することをお勧めします:あなたはあなたのスプライトの規模を高めるためにccTouchesBeganに間隔ベースのアクション/メソッドをスケジュールすることができ

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

(例えば毎秒0.1を大きくする)とccTouchesEndedでそのアクションをキャンセルし、スケールを元の値(1.0など)に戻します。スケジューリングのための

は(CocosNodeを拡張する任意のクラスで見つかった)その方法を見てみましょう:

- (無効)スケジュール:(SEL)■;

0

私は、cocos2DとSpaceManagerを使用してタッチでズームイン/ズームアウトを実装しました。すでに受け入れられている回答はうまく見えますが、すでにSpaceManagerを使用している場合は、どうすればこのようにできるかを示したかったのです。

注:このスニペットは、1回のスワップでズームイン/ズームアウトします。既に承認されている回答は、リリース時に縮小のみを行うようにこれを修正する方法を見てみることができます。

これは、私はそれをやった方法です:

#pragma mark touch 
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    NSLog(@"Touched began"); 
    CGPoint position = [touch locationInView: [touch view]]; 
    CGPoint positionLocal =[[CCDirector sharedDirector] convertToGL: position]; 

    cpShape *touchedShape = [smgr getShapeAt:positionLocal]; 
    if (touchedShape != nil) { 
    NSLog(@"something was touched"); 
    cpCCSprite *cpCCSOwner = (cpCCSprite *) touchedShape->data; 
    // Let's do 'pulse' special effect on whatever we just touched, just to give the user some feedback   
    cpFloat originalScale_ = cpCCSOwner.scale; 
    CCFiniteTimeAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];// zoom in 
    CCFiniteTimeAction *shrinkAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];// zoom out 
    CCSequence *actions = [CCSequence actions:zoomAction, shrinkAction, nil];// zoom in, then zoom out 
    [cpCCSOwner runAction:actions];// now 

    // Since I just grabbed the object, it should stop moving 
    cpCCSOwner.shape->body->v = cpvzero; 

    // Let's keep track of what we are touching. 
    cpCCSpriteTouched = cpCCSOwner; //<- I just added this to the class, you'll need something more sophisticated from non-trivial cases 


    } else { 
    NSLog(@"nothing was actually touched - you missed");  
    cpCCSpriteTouched = nil; 
    } 
    return YES; 
} 
関連する問題