我想将游戏活动转换为游戏片段。任何人都可以帮助我吗? 这是游戏活动:
public class Game extends BaseGameActivity implements IOnSceneTouchListener, Observer{
private static int CAMERA_HEIGHT = 480;
private static int CAMERA_WIDTH = 800;
private Camera mCamera;
private TiledTextureRegion ballTextureRegion;
private Texture paddleTexture;
private Texture ballTexture;
private Paddle paddle;
private GameHolder gameHolder;
private Object mCurViewMode;
public Engine onLoadEngine()
{
final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
CAMERA_WIDTH = defaultDisplay.getWidth();
CAMERA_HEIGHT = defaultDisplay.getHeight();
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
@Override
public void onLoadResources()
{
gameHolder = GameHolder.getInstance();
gameHolder.addObserver(this);
gameHolder.setGameActivity(this);
this.ballTexture = new Texture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.paddleTexture = new Texture(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegionFactory.setAssetBasePath("gfx/");
this.ballTextureRegion = TextureRegionFactory.createTiledFromAsset(this.ballTexture, this, "ball.png", 0, 0, 1, 1);
this.mEngine.getTextureManager().loadTexture(this.paddleTexture);
this.mEngine.getTextureManager().loadTexture(this.ballTexture);
}
@Override
public Scene onLoadScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene(1);
scene.setBackground(new ColorBackground(0f, 0f, 0f));
scene.setOnSceneTouchListener(this);
final Ball ball = new Ball(CAMERA_WIDTH/2, CAMERA_HEIGHT-30-16, this.ballTextureRegion, mEngine);
ball.setVelocity(100.0f, 100.0f);
scene.getTopLayer().addEntity(ball);
paddle = new Paddle(CAMERA_WIDTH/2, CAMERA_HEIGHT-50, CAMERA_HEIGHT/8, CAMERA_WIDTH/34);
final Brick[][] bricks = new Brick[5][5];
for (int i = 0; i < bricks.length; i++) {
for (int j = 0; j < bricks[0].length; j++) {
bricks[i][j]= new Brick(10+j*CAMERA_WIDTH/5, 10+i*CAMERA_HEIGHT/15 , CAMERA_HEIGHT/16, CAMERA_WIDTH/32);
scene.getTopLayer().addEntity(bricks[i][j]);
}
}
scene.getTopLayer().addEntity(paddle);
scene.getTopLayer().addEntity(ball);
scene.registerTouchArea(paddle);
/* The actual collision-checking. */
scene.registerUpdateHandler(new IUpdateHandler() {
public void reset() { }
public void onUpdate(final float pSecondsElapsed) {
if(ball.collidesWith(paddle)) {
ball.bounceWithRectangle(paddle);
}
else if (ball.getY() >= Game.getCAMERA_HEIGHT() - 30) {
scene.setBackground(new ColorBackground(255f, 0f, 0f));
Log.w("Game.java","Game Over");
}
else {
for (int i = 0; i < bricks.length; i++) {
for (int j = 0; j < bricks[0].length; j++) {
scene.setBackground(new ColorBackground(0f, 0f, 0f));
if(ball.collidesWith(bricks[i][j])) {
bricks[i][j].setPosition(CAMERA_HEIGHT+20, CAMERA_WIDTH+20);
scene.getTopLayer().removeEntity(bricks[i][j]);
ball.bounceWithRectangle(bricks[i][j]);
}
}
}
}
}
});
return scene;
}
public static int getCAMERA_HEIGHT() {
return CAMERA_HEIGHT;
}
public static int getCAMERA_WIDTH() {
return CAMERA_WIDTH;
}
@Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
paddle.setPosition(pSceneTouchEvent.getX() - paddle.getWidth() / 2, Game.getCAMERA_HEIGHT()-30);
return true;
}
protected void onPause() {
super.onPause();
//commented on error
//gameHolder.setGameState(gameHolder.getPausedGameState());
}
@Override
public void update(Observable observable, Object data) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
您可以将活动更改为这样的片段:
public class FragmentClass extends Fragment
{
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = (View) inflater.inflate(R.layout.layout_history, container,false);
//Do all things which you want to implement //enter code here
return view;
}
}
您需要扩展Fragment并覆盖其onCreateView(LayoutInflater, ViewGroup, Bundle)
方法。
使用如上所述的布局和返回视图对其进行充气。
您还可以使用其视图创建此片段的成员,并使用以下方式传递上下文:
getActivity();
如果您想使用findViewById(int)
,那么您可以使用它:
getView().findViewById(int);
如果您想了解更多相关信息,请转到:http://developer.android.com/guide/components/fragments.html &安培; http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
您可以创建如下的自定义片段。完成所有这些后,您可以使用片段类中的getActivity()来使用您的方法。
关于你的方法的错误,你必须使用当前片段类的上下文 例如,如果要使用TextView:
TextView tv = (TextView) context.findViewById(R.id.textView);
我正在展示如何使用BaseActivity制作片段的示例:
public class MainActivity extends BaseActivity
{
private FragmentClass fragmentClass;
private ViewPager mViewPager;
private SlidingTabLayout mSlidingTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Do all stub
populateView();
}
private void populateView()
{
mViewPager = (ViewPager) findViewById(R.id.pager1);
mViewPager.setOffscreenPageLimit(2);
mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setDividerColors(color);
mSlidingTabLayout.setCustomTabView(R.layout._custom_tab, 0);
mSlidingTabLayout.setViewPager(mViewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter
{
public ViewPagerAdapter(FragmentManager fm)
{
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int pos)
{
if(pos==0)
{
fragmentClass = new FragmentClass();
return fragmentClass;
}
return null;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return 1;
}
}
}
从developer.android中添加两个类: