Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Cocos2D-Android-1之源碼詳解:6.ClickAndMoveTest

Cocos2D-Android-1之源碼詳解:6.ClickAndMoveTest

編輯:關於Android編程

package org.cocos2d.tests;

import org.cocos2d.actions.CCProgressTimer;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.interval.CCFadeIn;
import org.cocos2d.actions.interval.CCFadeOut;
import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCRotateTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.ccColor4B;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

//
// Click and Move demo
// a cocos2d example
// http://www.cocos2d-iphone.org
//
public class ClickAndMoveTest extends Activity {//點擊和移動
    // private static final String LOG_TAG = ClickAndMoveTest.class.getSimpleName();

    // private static final boolean DEBUG = true;
	public static ClickAndMoveTest app;//自己類的引用
    private CCGLSurfaceView mGLSurfaceView;//新建view

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = this;//把唯一的引用傳到類靜態量裡
        //無題、全屏、不黑
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //新建view並把this放入導演變量集合中
        mGLSurfaceView = new CCGLSurfaceView(this);
        setContentView(mGLSurfaceView);//把view映射到activity中
        
        // attach the OpenGL view to a window
        CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把view給導演,讓導演來演

        // set landscape mode
        CCDirector.sharedDirector().setLandscape(false);//設置景觀模式
//又是3個通俗的設置
        // show FPS
        CCDirector.sharedDirector().setDisplayFPS(true);

        // frames per second
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);

        CCScene scene = CCScene.node();
        scene.addChild(new MainLayer(), 2);

        // Make the Scene active
        CCDirector.sharedDirector().runWithScene(scene);//把場景給導演,讓他來演,導演會把這個放到activity中
    }
//老4件..
    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onPause() {
        super.onPause();

        CCDirector.sharedDirector().onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

        CCDirector.sharedDirector().onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        CCDirector.sharedDirector().end();
    }

    static class MainLayer extends CCLayer {//主圖層
        static final int kTagSprite = 1;//定義一個tag是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);//添加孩子,tag作為1
            sprite.setPosition(CGPoint.make(20, 150));//設置點

            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);//添加進去這2個
            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);//得到剛才那個tag是1的精靈
            s.stopAllActions();//停止所有的動作
            s.runAction(CCMoveTo.action(1.0f, convertedLocation));//移動到點擊的點
           
            CGPoint pnt = s.getPosition();//得到點

            float at = CGPoint.ccpCalcRotate(pnt, convertedLocation);//得到兩點的旋轉向量

            s.runAction(CCRotateTo.action(1, at));//旋轉
            
            progressTimer.setPercentage(10.0f + progressTimer.getPercentage());//進度條設置進度

            return CCTouchDispatcher.kEventHandled;//返回數據
        }

    }

}

上一篇:/kf/201512/452997.html

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved