2011-11-12 3 views
0

私はcocos2dする新たなんだと私は私はすでにこのような何かを試してみたスプライトを触ってきたかどうかをチェックし、Javaでコードを書くのですかどのように私は思っていた。..cocos2dアンドロイド

@Override 
public boolean ccTouchesEnded(MotionEvent event) 
{ 

    CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY())); 

    if ((location.x == zom.getPosition().x) && (location.y == zom.getPosition().y)) 
    { 
    CCSprite projectile = CCSprite.sprite("bullet.png"); 
    projectile.setPosition(CGPoint.ccp(player.getPosition().x,player.getPosition().y)); 
    addChild(projectile); 
    float length = (float)Math.sqrt((100 * 100) + (100 * 100)); 
    float velocity = 100.0f/1.0f; 
    float realMoveDuration = length/velocity; 
    projectile.runAction(CCSequence.actions(
      CCMoveTo.action(realMoveDuration, CGPoint.ccp(location.x, location.y)), 
      CCCallFuncN.action(this, "spriteMoveFinished"))); 
     if ((projectile.getPosition().x == location.x) && (projectile.getPosition().y == location.y)) 
     { 
      removeChild(projectile, true); 
     } 
    } 
+0

ちょうど好奇心をコンストラクタにこれを追加してください、あなたの関数は、ブール値を返しません。 – Raptor

答えて

0

私はcocos2dマスターではありませんが、ロジックがコードをチェックするのに少しずれているようです。タッチポイントがスプライトの現在の領域にあるかどうかをチェックしたいとします(((location.x >= sprite.start.x && location.x <= sprite.width) && ((location.y >= sprite.start.y && location.y <= sprite.height)

スプライトクラスを拡張してポイントがスプライトエリア(float isInSpriteArea(CGPoint point))あなただけのスプライトにポイントを渡すことができますこの方法、それはこのケースで触れている場合、それはあなたを伝えることができ

1

をそのための非常に最善の解決策があります使用します。。。

sprite.getBoundingBox.contains(x,y); 

ここで、xおよびyは、タッチされた位置の位置である。

1

私はこれがあなたを助けてくれることを願っています。私はこの方法を使用して、特定の邪悪に対してタッチイベントを処理します。

public boolean ccTouchesEnded(MotionEvent event) { 
     CGPoint location = CCDirector.sharedDirector().convertToGL(
       CGPoint.ccp(event.getX(), event.getY())); 
     if (CGRect.containsPoint((newGame1.getBoundingBox()), location)) { 

      newGame(); 

     } 

     return super.ccTouchesEnded(event); 
    } 

必要に応じて

this.setIsTouchEnabled(true); 
関連する問題