이벤트 발생과 처리가 필요하므로
1. Button 생성
먼저 버튼으로 쓸 이미지를 준비한다.
game start버튼을 보통(?)/클릭시 두가지로 준비했다.
이미지는 일반 android project와 다르게 assets/gfx 디렉토리에 넣는다. 그 이유는 나중에 포스팅하겠다.
소스에서 버튼이미지를 불러온다. 리소스를 가져오는 것이므로
아잌ㅋ 소스까지 다 스샷찍어서 올리기 귀찮다. 복붙해야
private BuildableBitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mFace1TextureRegion;
private ITextureRegion mFace2TextureRegion;
@Override
protected void onCreateResources() {
// TODO Auto-generated method stub
this.mBitmapTextureAtlas =
new BuildableBitmapTextureAtlas(this.getTextureManager(), 512, 512);
this.mFace1TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "gfx/btn_start.bmp");
this.mFace2TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "gfx/btn_start_over.bmp");
try {
this.mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
this.mBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Debug.e(e);
}
}
그리고 scene을 생성하면서 버튼스프라이트를 생성한다.
@Override
protected Scene onCreateScene() {
// TODO Auto-generated method stub
scene = new Scene();
scene.setBackground(new Background(0x0, 0x0, 0xffffff)); // 기존 소스
//버튼스프라이트를 생성하여 scene에 붙인다.
face = new ButtonSprite(100, 100, this.mFace1TextureRegion, this.mFace2TextureRegion, this.getVertexBufferObjectManager(), this);
scene.registerTouchArea(face);
scene.attachChild(face);
scene.setTouchAreaBindingOnActionDownEnabled(true);
return scene;
}
2. Event 구현
버튼터치 이벤트를 구현하기 위해 implement로 OnClickListener를 추가해준다.
public class MainActivity extends SimpleBaseGameActivity implements OnClickListener
그러면 필요한 메소드가 없다고 에러가 난다. 추가해주고 그기를 구현해준다.
@Override
public void onClick(final ButtonSprite pButtonSprite, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
if(pButtonSprite==(ButtonSprite)face)
Toast.makeText(MainActivity.this, "Clicked Start Button", Toast.LENGTH_LONG).show();
}
});
}
이렇게 하고 실행시켜보면~!~!~!
< 버튼 클릭시 roll-over > < 버튼 클릭 후 이벤트 >
잘 되는군.
'개발Study > Android_APP' 카테고리의 다른 글
android app 개발환경 구축 및 프로젝트 시작 (0) | 2022.02.20 |
---|---|
AndEngine4 - Scene의 전환 (0) | 2015.02.18 |
AndEngine3 - Scene Touch Event (0) | 2015.02.18 |
AndEngine1 - 프로젝트 생성 (0) | 2015.02.17 |
xml error (0) | 2015.01.01 |
댓글