Reputation: 219
I want to convert Game activity into Game fragment . Can any one help me in this?
This is game activity:
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
}
}
Upvotes: 0
Views: 3630
Reputation: 484
You can change your activity into fragment like this :
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;
}
}
You need to extends Fragment and override its onCreateView(LayoutInflater, ViewGroup, Bundle)
method.
Inflate it using layout and return view like above.
You can also create member of this fragment using its view and pass context using:
getActivity();
If you want to use findViewById(int)
then you would use it like :
getView().findViewById(int);
If you want to know more about this then go to: http://developer.android.com/guide/components/fragments.html & http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
You can create custom fragment like below. After all of this you can use your methods using getActivity() from fragment class.
And about error of your methods then you have to use context of current fragment class.
Example if you want to use TextView then:
TextView tv = (TextView) context.findViewById(R.id.textView);
I am showing example how to make fragment using 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;
}
}
}
Add Two classes from developer.android :
Upvotes: 3