您好,登錄后才能下訂單哦!
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 {//點(diǎn)擊和移動(dòng)
// 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;//把唯一的引用傳到類靜態(tài)量里
//無題、全屏、不黑
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放入導(dǎo)演變量集合中
mGLSurfaceView = new CCGLSurfaceView(this);
setContentView(mGLSurfaceView);//把view映射到activity中
// attach the OpenGL view to a window
CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把view給導(dǎo)演,讓導(dǎo)演來演
// set landscape mode
CCDirector.sharedDirector().setLandscape(false);//設(shè)置景觀模式
//又是3個(gè)通俗的設(shè)置
// 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);//把場景給導(dǎo)演,讓他來演,導(dǎo)演會(huì)把這個(gè)放到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;//定義一個(gè)tag是1
CCProgressTimer progressTimer;//進(jìn)程時(shí)間
public MainLayer() {
this.setIsTouchEnabled(true);//可點(diǎn)擊
CCSprite sprite = CCSprite.sprite("grossini.png");//建立一個(gè)精靈
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));//設(shè)置點(diǎn)
sprite.runAction(CCJumpTo.action(4, CGPoint.make(300, 48), 100, 4));
//執(zhí)行跳躍動(dòng)畫
CCLabel lbl1 = CCLabel.makeLabel("Click on the screen", "DroidSans", 24);
//放個(gè)標(biāo)簽,內(nèi)容、字體、大小
CCLabel lbl2 = CCLabel.makeLabel("to move and rotate Grossini", "DroidSans", 16);//同理
addChild(lbl1, 0);
addChild(lbl2, 1);//添加進(jìn)去這2個(gè)
lbl1.setPosition(CGPoint.ccp(160, 240));
lbl2.setPosition(CGPoint.ccp(160, 200));//再回頭設(shè)置位置
progressTimer = CCProgressTimer.progress("iso.png");//進(jìn)度條效果
this.addChild(progressTimer, 10);//把進(jìn)度條弄進(jìn)去
progressTimer.setPosition(160, 100);//設(shè)置位置
progressTimer.setType(CCProgressTimer.kCCProgressTimerTypeVerticalBarTB);
//設(shè)置類型-垂直的進(jìn)度條
progressTimer.setPercentage(50.0f);//設(shè)置現(xiàn)在的百分比
layer.runAction(CCRepeatForever.action(CCSequence.actions(CCFadeIn.action(1), CCFadeOut.action(1))));//圖層閃爍
}
@Override
public boolean ccTouchesBegan(MotionEvent event) {//觸動(dòng)事件
CGPoint convertedLocation = CCDirector.sharedDirector()
.convertToGL(CGPoint.make(event.getX(), event.getY()));//得到點(diǎn)
CCNode s = getChildByTag(kTagSprite);//得到剛才那個(gè)tag是1的精靈
s.stopAllActions();//停止所有的動(dòng)作
s.runAction(CCMoveTo.action(1.0f, convertedLocation));//移動(dòng)到點(diǎn)擊的點(diǎn)
CGPoint pnt = s.getPosition();//得到點(diǎn)
float at = CGPoint.ccpCalcRotate(pnt, convertedLocation);//得到兩點(diǎn)的旋轉(zhuǎn)向量
s.runAction(CCRotateTo.action(1, at));//旋轉(zhuǎn)
progressTimer.setPercentage(10.0f + progressTimer.getPercentage());//進(jìn)度條設(shè)置進(jìn)度
return CCTouchDispatcher.kEventHandled;//返回?cái)?shù)據(jù)
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。