您好,登錄后才能下訂單哦!
package org.cocos2d.tests;
import javax.microedition.khronos.opengles.GL10;
import org.cocos2d.actions.interval.CCRotateBy;
import org.cocos2d.config.ccMacros;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCDrawingPrimitives;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class DrawPrimitivesTest extends Activity {//繪畫原語測試
// private static final String LOG_TAG = DrawPrimitivesTest.class.getSimpleName();
private CCGLSurfaceView mGLSurfaceView;//建立view
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//3個設置//同之前
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mGLSurfaceView = new CCGLSurfaceView(this);//創建surface
setContentView(mGLSurfaceView);//映射view
// attach the OpenGL view to a window
CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把view給導演類
// set landscape mode
CCDirector.sharedDirector().setLandscape(false);//不橫屏
// show FPS
CCDirector.sharedDirector().setDisplayFPS(true);//實時顯示每秒多少幀
// frames per second
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);//標定顯示多少幀
CCScene scene = CCScene.node();
scene.addChild(nextAction());//創建節點
scene.runAction(CCRotateBy.action(4, -360));//旋轉-360度,在4秒鐘
// Make the Scene active
CCDirector.sharedDirector().runWithScene(scene);//導演開始把圖層上面的元素給view來演
}
@Override
public void onStart() {//下面3個老方法不贅述
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();
// CCTextureCache.sharedTextureCache().removeAllTextures();
}
static int sceneIdx = -1;
static Class<?> transitions[] = {//只有一個...還用個數組..
Test1.class,
};
public static CCLayer nextAction() {
sceneIdx++;
sceneIdx = sceneIdx % transitions.length;
return restartAction();
}
public static CCLayer backAction() {
sceneIdx--;
int total = transitions.length;
if (sceneIdx < 0)
sceneIdx += total;
return restartAction();
}
public static CCLayer restartAction() {//重新返回一個這個實例
try {
Class<?> c = transitions[sceneIdx];
return (CCLayer) c.newInstance();
} catch (Exception e) {
return null;
}
}
public static class TestDemo extends CCLayer {//測試
public TestDemo() {
CGSize s = CCDirector.sharedDirector().winSize();//得到大小
CCMenuItemImage item1 = CCMenuItemImage.item("b1.png", "b2.png", this, "backCallback");//第一個菜單項目,其實是個按鈕,第一個是沒按,第二個是按下,然后是this里的方法,然后方法名字是最后一個參數,java有反射機制,所以他才敢這樣寫..但是這樣不好混淆,后面2個按鈕同理
CCMenuItemImage item2 = CCMenuItemImage.item("r1.png", "r2.png", this, "restartCallback");
CCMenuItemImage item3 = CCMenuItemImage.item("f1.png", "f2.png", this, "nextCallback");
CCMenu menu = CCMenu.menu(item1, item2, item3);//把這3個菜單項的按鈕放進一個菜單
menu.setPosition(CGPoint.make(0, 0));//給菜單設置位置,0.0就是在原點..
item1.setPosition(CGPoint.make(s.width / 2 - 100, 30));//只要按鈕項目設對就行了,所以菜單的坐標就只是個0.0
item2.setPosition(CGPoint.make(s.width / 2, 30));
item3.setPosition(CGPoint.make(s.width / 2 + 100, 30));
addChild(menu, -1);//最后把菜單作為activity的子類,而且顯示順序是-1,有可能被遮擋的
}
/*
* After setting the screen orientation to landscape,
* the Activity will be restarted, so it seems we should not call setLandscape here
* this is a bug, we should make full use of android's capability, but not partly.
*/
public void restartCallback(Object sender) {//重新加載,并切換觀景模式,橫豎屏
boolean landscape = CCDirector.sharedDirector().getLandscape();//得到觀景模式
CCDirector.sharedDirector().setLandscape(!landscape);//切換觀景模式
CCScene s = CCScene.node();//生成圖層節點
s.addChild(restartAction());
CCDirector.sharedDirector().runWithScene(s);//開始畫這個圖層
}
public void nextCallback(Object sender) {//執行下一個class并切換觀景模式
boolean landscape = CCDirector.sharedDirector().getLandscape();
CCDirector.sharedDirector().setLandscape(!landscape);
CCScene s = CCScene.node();
s.addChild(nextAction());
CCDirector.sharedDirector().runWithScene(s);
}
public void backCallback(Object sender) {//執行上一個切換觀景模式
boolean landscape = CCDirector.sharedDirector().getLandscape();
CCDirector.sharedDirector().setLandscape(!landscape);
CCScene s = CCScene.node();
s.addChild(backAction());
CCDirector.sharedDirector().runWithScene(s);
}
String title() {
return "No title";
}
}
public static class Test1 extends TestDemo {//測試1
public static CCLayer node() {
return new Test1();
}
//
// TIP:
// Every CocosNode has a "draw" method.
// In the "draw" method you put all the code that actually draws your node.
// And Test1 is a subclass of TestDemo, which is a subclass of Layer, which is a subclass of CocosNode.
//
// As you can see the drawing primitives aren't CocosNode objects. They are just helper
// functions that let's you draw basic things like: points, line, polygons and circles.
//
//
// TIP:
// Don't draw your stuff outside the "draw" method. Otherwise it won't get transformed.
//
//
// TIP:
// If you want to rotate/translate/scale a circle or any other "primtive", you can do it by rotating
// the node. eg:
// this.rotation = 90;
//
public void draw(GL10 gl) {//一個繪畫類
CGSize s = CCDirector.sharedDirector().winSize();//還是得到屏幕大小
// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Anti-Aliased
gl.glEnable(GL10.GL_LINE_SMOOTH);//設置線平滑模式
CCDrawingPrimitives.ccDrawLine(gl, CGPoint.ccp(0, 0), CGPoint.ccp(s.width, s.height));//運用繪畫基元來畫線
// line: color, width, aliased
// glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
// GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
gl.glDisable(GL10.GL_LINE_SMOOTH);//撤銷平滑線模式
gl.glLineWidth(5.0f);//設置線寬5.0
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//設置顏色
CCDrawingPrimitives.ccDrawLine(gl, CGPoint.ccp(0, s.height), CGPoint.ccp(s.width, 0));//然后又是基元繪畫線
// TIP:
// If you are going to use always the same color or width, you don't
// need to call it before every draw
//
// Remember: OpenGL is a state-machine.
// draw big point in the center
gl.glPointSize(64);//設置點大小
gl.glColor4f(0.0f, 0.0f, 1.0f, 0.5f);//基元顏色
CCDrawingPrimitives.ccDrawPoint(gl, CGPoint.make(s.width / 2, s.height / 2));//基元繪畫,畫點
// draw 4 small points
CGPoint points[] = {CGPoint.ccp(60, 60), CGPoint.ccp(70, 70), CGPoint.ccp(60, 70), CGPoint.ccp(70, 60)};//點數組
gl.glPointSize(4);//設置點大小
gl.glColor4f(0.0f, 1.0f, 1.0f, 1.0f);//基元顏色
CCDrawingPrimitives.ccDrawPoints(gl, points, 4);//基元畫點
// draw a green circle with 10 segments
gl.glLineWidth(16);//設置線寬
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);//基元顏色
CCDrawingPrimitives.ccDrawCircle(gl, CGPoint.make(s.width / 2, s.height / 2), 100, 0, 10, false);//基元畫線
// draw a green circle with 50 segments with line to center
gl.glLineWidth(2);//設置線寬
gl.glColor4f(0.0f, 1.0f, 1.0f, 1.0f);//顏色
CCDrawingPrimitives.ccDrawCircle(gl, CGPoint.make(s.width / 2, s.height / 2), 50, ccMacros.CC_DEGREES_TO_RADIANS(90), 50, true);//基元畫圓..,參數:基元,中心點坐標,半徑,弧度值(參數是角度),段數,是否畫線到圓心
// open yellow poly
gl.glColor4f(1.0f, 1.0f, 0.0f, 1.0f);//設置顏色
gl.glLineWidth(10);//設置線寬
CGPoint vertices[] = {CGPoint.ccp(0, 0), CGPoint.ccp(50, 50), CGPoint.ccp(100, 50), CGPoint.ccp(100, 100), CGPoint.ccp(50, 100)};//設置點數組
CCDrawingPrimitives.ccDrawPoly(gl, vertices, 5, false);//繪制多邊形
// closed purple poly
gl.glColor4f(1.0f, 0.0f, 1.0f, 1.0f);//顏色
gl.glLineWidth(2);//線寬
CGPoint vertices2[] = {CGPoint.ccp(30, 130), CGPoint.ccp(30, 230), CGPoint.ccp(50, 200)};//點數組
CCDrawingPrimitives.ccDrawPoly(gl, vertices2, 3, true);//多邊形
// draw quad bezier path
CCDrawingPrimitives.ccDrawQuadBezier(gl, CGPoint.make(0,s.height), CGPoint.make(s.width/2,s.height/2), CGPoint.make(s.width, s.height), 50);//繪制班塞爾曲線...一種計算機用來畫曲線的曲線,,參數:起點,控點,結束點,頂點數
// draw cubic bezier path
CCDrawingPrimitives.ccDrawCubicBezier(gl, CGPoint.make(s.width/2, s.height/2), CGPoint.make(s.width/2+30, s.height/2+50),
CGPoint.make(s.width/2+60, s.height/2-50), CGPoint.make(s.width, s.height/2),100);//繪制立體貝塞爾曲線。更高端了,參數:起點,控點1,控點2,結束點,頂點數..
// restore original values//基元恢復數據
gl.glLineWidth(1);//線寬1
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);//顏色黑
gl.glPointSize(1);//點大小1
}
public String title() {
return "draw primitives";
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。