cocos2d for android - 터치 이벤트

 

static class MainLayer extends CCLayer {
       static final int kTagSprite = 1;
       CCProgressTimer  progressTimer;
 
       public MainLayer() {
 
           // 터치 이벤트 활성
           this.setIsTouchEnabled(true);
 
           // 스프라이트 생성(주인공이라고 하지)
           CCSprite sprite = CCSprite.sprite("grossini.png");
 
           // 전체 화면을 깜밖일 레이어 생성
           CCLayer layer = CCColorLayer.node(new ccColor4B(255, 255, 0, 255));
           addChild(layer, -1);
 
           // 스프라이트 등록
           addChild(sprite, 1, kTagSprite);
           sprite.setPosition(CGPoint.make(20, 150));
 
           // 액션 실행
           // 4초동안 300, 48 만큼 100 높이로 점프하면서 옴
           sprite.runAction(CCJumpTo.action(4, CGPoint.make(300, 48), 100, 4));
 
           CCLabel lbl1 = CCLabel.makeLabel("Click on the screen", "DroidSans", 24);
           CCLabel lbl2 = CCLabel.makeLabel("to move and rotate Grossini", "DroidSans", 16);
 
           addChild(lbl1, 0);
           addChild(lbl2, 1);
           lbl1.setPosition(CGPoint.ccp(160, 240));
           lbl2.setPosition(CGPoint.ccp(160, 200));
           
           progressTimer = CCProgressTimer.progress("iso.png");
           this.addChild(progressTimer, 10);
           progressTimer.setPosition(160, 100);
           progressTimer.setType(CCProgressTimer.kCCProgressTimerTypeVerticalBarTB);
           progressTimer.setPercentage(50.0f);
           
           layer.runAction(CCRepeatForever.action(CCSequence.actions(CCFadeIn.action(1), CCFadeOut.action(1))));
       }
 
       @Override
       public boolean ccTouchesBegan(MotionEvent event) {
           // 현재 찍힌 좌표 계산
           CGPoint convertedLocation = CCDirector.sharedDirector()
               .convertToGL(CGPoint.make(event.getX(), event.getY()));
 
           // 노드 스프라이트 얻어오기
           CCNode s = getChildByTag(kTagSprite);
           // 모든 액션 중지
           s.stopAllActions();
           // 액션 실행(찍힌 위치로 가라)
           s.runAction(CCMoveTo.action(1.0f, convertedLocation));
          
           // 위치 얻어오기
           CGPoint pnt = s.getPosition();
 
           // 찍힌 위치를 기준으로 회전 각 계산
           float at = CGPoint.ccpCalcRotate(pnt, convertedLocation);
 
           // 회전하면서 가라(그냥 가면 이상하자너)
           s.runAction(CCRotateTo.action(0.1f, at));
           
           progressTimer.setPercentage(10.0f + progressTimer.getPercentage());
 
           return CCTouchDispatcher.kEventHandled;
       }
 
   }

댓글

이 블로그의 인기 게시물

ViewPager offscreenPageLimit 메모리 상태

ASP에서 C# 언어로 만든 DLL 호출

eskimo esim 사용기 - 일본 / 오사카(23/08/06 ~ 23/08/08)