1、新建一個工程
2、導jar包到libs目錄下,並Add to buildpath
3、到所需要的圖片到assets目錄下(註意,一定要有那個名為fps。。.png的圖片,因為框架底層用到瞭這個名字的圖片)
4、MainActivity
package com.example.cocos2dtest; import org.cocos2d.nodes.CCDirector; import org.cocos2d.opengl.CCGLSurfaceView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CCGLSurfaceView view = new CCGLSurfaceView(this); setContentView(view); CCDirector.sharedDirector().attachInView(view); CCDirector.sharedDirector().setDisplayFPS(true); CCDirector.sharedDirector().setAnimationInterval(1.0f/60); CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait); CCDirector.sharedDirector().setScreenSize(480, 320); MyCCScene scene = new MyCCScene(); CCDirector.sharedDirector().runWithScene(scene); } @Override protected void onDestroy() { CCDirector.sharedDirector().end(); super.onDestroy(); } @Override protected void onPause() { CCDirector.sharedDirector().pause(); super.onPause(); } @Override protected void onResume() { CCDirector.sharedDirector().resume(); super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
5、MyCCScene
package com.example.cocos2dtest; import org.cocos2d.layers.CCLayer; import org.cocos2d.layers.CCScene; import org.cocos2d.nodes.CCSprite; import android.util.Log; public class MyCCScene extends CCScene { public MyCCScene() { init(); } private void init() { CCLayer layer = CCLayer.node(); CCSprite sss = CCSprite.sprite(cover.jpg); sss.setAnchorPoint(0,0); //①被添加的子元素②重要性(重要性高的覆蓋重要性低的 ) ③標簽(相當於給精靈加瞭一個ID) layer.addChild(sss, 1, 1); CCSprite sprite = CCSprite.sprite(home_button.png); sprite.setAnchorPoint(0f, 0f); Log.v(getAnchorPoint=========+sprite.getOpacity(), getPosition=========+sprite.getPosition()); sprite.setPosition(100,200); layer.addChild(sprite, 2, 2); CCSprite s = CCSprite.sprite(home_button.png); s.setAnchorPoint(0f, 0f); s.setPosition(50,50); // s.setFlipX(true);//設置x方向鏡像 // s.setFlipY(true); s.setOpacity(100);//設置透明度 layer.addChild(s, 3, 3); this.addChild(layer); } // private void init() { // CCLayer layer = CCLayer.node(); // CCSprite sprite = CCSprite.sprite(home_button.png); // //// sprite.setPosition(0f, 0f); // sprite.setAnchorPoint(0f, 0f); // layer.addChild(sprite); // // // this.addChild(layer); // // } }
———————————————————————————————————–
需要註意的是以下幾點:
1、setAnchorPoint()和setPosition()都可以改變精靈(sprite)的位置。
2、關於鏡像的說明:
3、這個實例的源代碼下載地址:
https://download.csdn.net/detail/caihongshijie6/6875277