package org.cocos2d.tests;
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)巨野免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
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;//把唯一的引用傳到類靜態(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放入導演變量集合中
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);//設(shè)置景觀模式
//又是3個通俗的設(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);//把場景給導演,讓他來演,導演會把這個放到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));//設(shè)置點
sprite.runAction(CCJumpTo.action(4, CGPoint.make(300, 48), 100, 4));
//執(zhí)行跳躍動畫
CCLabel lbl1 = CCLabel.makeLabel("Click on the screen", "DroidSans", 24);
//放個標簽,內(nèi)容、字體、大小
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));//再回頭設(shè)置位置
progressTimer = CCProgressTimer.progress("iso.png");//進度條效果
this.addChild(progressTimer, 10);//把進度條弄進去
progressTimer.setPosition(160, 100);//設(shè)置位置
progressTimer.setType(CCProgressTimer.kCCProgressTimerTypeVerticalBarTB);
//設(shè)置類型-垂直的進度條
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) {//觸動事件
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);//得到兩點的旋轉(zhuǎn)向量
s.runAction(CCRotateTo.action(1, at));//旋轉(zhuǎn)
progressTimer.setPercentage(10.0f + progressTimer.getPercentage());//進度條設(shè)置進度
return CCTouchDispatcher.kEventHandled;//返回數(shù)據(jù)
}
}
}