본문 바로가기

Cocos2d

Cocos2d Android : 1장 . Scene 만들기

//next 이미지 터치시 현재 scene 다시 불러주고 , end 이미지터치시 종료하는 간단한 소스입니다
//asset 폴더에 next.png , end.png 이미지를 만드셔서 넣어주어야 합니다
//cocos2d 라이브러리를 첨부했습니다. 외부 라이브러리 import하는 방법은 생략하겠습니다.

CoCosActivity.java
-------------------------------------------------------------------------------------------------
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
 
 public class CoCosActivity extends Activity {
private CCGLSurfaceView mGLSurfaceView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    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);
mGLSurfaceView = new CCGLSurfaceView(this);
CCDirector director = CCDirector.sharedDirector();
director.attachInView(mGLSurfaceView);
setContentView(mGLSurfaceView);
CCDirector.sharedDirector().setDisplayFPS(false);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
CCScene scene = StartLayer.scene();
CCDirector.sharedDirector().runWithScene(scene);
    }

-------------------------------------------------------------------------------------------------

 StartLayer.java 
------------------------------------------------------------------------------------------------ 
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.types.CGPoint;

import android.view.MotionEvent;

public class  StartLayer extends CCLayer {
CCSprite nextSceneSprite;
CCSprite endSceneSprite;
public  StartLayer (){
this.setIsTouchEnabled(true);
setBackGround();
}
private void setBackGround() {
nextSceneSprite = CCSprite.sprite("next.png");
nextSceneSprite.setPosition(240f,500f);
endSceneSprite = CCSprite.sprite("end.png");
endSceneSprite.setPosition(240f,350f);
this.addChild(nextSceneSprite);
this.addChild(endSceneSprite);
}
public static CCScene scene() {
CCScene scene = CCScene.node();
CCLayer layer = new StartLayer();
scene.addChild(layer);
return scene;
}
@Override
public boolean ccTouchesBegan(MotionEvent event) {
 CGPoint startLocation =  
  CCDirector.sharedDirector().convertToGL(CGPoint.make(event.getX(), event.getY()));
 if ( nextSceneSprite.getBoundingBox().contains(startLocation.x, startLocation.y)){
CCScene scene = StartLayer.scene();
CCDirector.sharedDirector().replaceScene(scene);
 }
 if ( endSceneSprite.getBoundingBox().contains(startLocation.x, startLocation.y)){
 CCDirector.sharedDirector().getActivity().finish();
 }
  
return super.ccTouchesBegan(event);
}

}
------------------------------------------------------------------------------------------------