热门IT资讯网

Cocos2D-Android-1之源码详解:6.ClickAndMoveTest

发表于:2024-11-30 作者:热门IT资讯网编辑
编辑最后更新 2024年11月30日,package org.cocos2d.tests;import org.cocos2d.actions.CCProgressTimer;import org.cocos2d.actions.base

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;//返回数据

}


}


}


0