Skip to content

Instantly share code, notes, and snippets.

@jrgleason
Created January 3, 2013 17:39
Show Gist options
  • Save jrgleason/4445255 to your computer and use it in GitHub Desktop.
Save jrgleason/4445255 to your computer and use it in GitHub Desktop.
Adding the finished AndroidApplication, Screen, and Android Manifest Classes.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.gleason.gametutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="org.gleason.gametutorial.GameActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package org.gleason.gametutorial;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import android.os.Bundle;
import android.view.Menu;
public class GameActivity extends AndroidApplication {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useAccelerometer = true;
config.useCompass = false;
config.useWakelock = true;
config.useGL20 = true;
SimpleGame game = new SimpleGame();
initialize(game, config);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
}
package org.gleason.gametutorial;
import javax.microedition.khronos.opengles.GL10;
import org.gleason.gametutorial.model.ArenaBarrier;
import org.gleason.gametutorial.model.Hero;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class GameScreen implements Screen {
private World world;
private Box2DDebugRenderer debugRenderer;
private OrthographicCamera camera;
private Hero hero;
private int count;
float accelX;
float accelY;
float accelZ;
public GameScreen() {
count = 0;
Vector2 gravity = new Vector2(0, -10f);
world = new World(gravity, true);
// Setup our hero
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(50f, 70f);
bodyDef.angle = 0;
hero = new Hero();
hero.setBody(world.createBody(bodyDef));
hero.createFixture();
// Here we setup the walls
BodyDef floorBodyDef = new BodyDef();
floorBodyDef.type = BodyType.StaticBody;
floorBodyDef.position.set((Gdx.graphics.getWidth() / 2), 10);
floorBodyDef.angle = 0;
ArenaBarrier floor = new ArenaBarrier(
(Gdx.graphics.getWidth() / 2) - 10, 0);
floor.setBody(world.createBody(floorBodyDef));
floor.createFixture();
BodyDef roofBodyDef = new BodyDef();
roofBodyDef.type = BodyType.StaticBody;
roofBodyDef.position.set(Gdx.graphics.getWidth() / 2,
Gdx.graphics.getHeight() - 10);
roofBodyDef.angle = 0;
ArenaBarrier roof = new ArenaBarrier(
(Gdx.graphics.getWidth() / 2 - 10), 0);
roof.setBody(world.createBody(roofBodyDef));
roof.createFixture();
BodyDef leftBodyDef = new BodyDef();
leftBodyDef.type = BodyType.StaticBody;
leftBodyDef.position.set(10, Gdx.graphics.getHeight() / 2);
leftBodyDef.angle = 0;
ArenaBarrier left = new ArenaBarrier(0,
(Gdx.graphics.getHeight() / 2) - 10);
left.setBody(world.createBody(leftBodyDef));
left.createFixture();
BodyDef rightBodyDef = new BodyDef();
rightBodyDef.type = BodyType.StaticBody;
rightBodyDef.position.set(Gdx.graphics.getWidth() - 10,
Gdx.graphics.getHeight() / 2);
rightBodyDef.angle = 0;
ArenaBarrier right = new ArenaBarrier(0,
Gdx.graphics.getHeight() / 2 - 10);
right.setBody(world.createBody(rightBodyDef));
right.createFixture();
camera = new OrthographicCamera();
camera.viewportHeight = Gdx.graphics.getHeight();
camera.viewportWidth = Gdx.graphics.getWidth();
camera.position.set(camera.viewportWidth * .5f,
camera.viewportHeight * .5f, 0f);
camera.update();
debugRenderer = new Box2DDebugRenderer();
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render(float arg0) {
// TODO Auto-generated method stub
itterate();
count++;
}
private void itterate() {
float timeStep = 1.0f / 45.0f;
int velocityIterations = 2;
int positionIterations = 8;
float accelX = Gdx.input.getAccelerometerX();
float accelY = Gdx.input.getAccelerometerY();
float accelZ = Gdx.input.getAccelerometerZ();
if (count != 0) {
if (accelX != this.accelX) {
// X has change
this.accelX = accelX;
float rad = accelX / 10;
hero.setTilt(-rad);
}
if (accelY != this.accelY) {
// y has changed
this.accelY = accelY;
float rad = accelY / 10;
hero.getBody().setTransform(hero.getBody().getPosition(), rad);
}
if (accelZ != this.accelZ) {
// Z has changed
this.accelZ = accelZ;
}
}
hero.startBody();
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(timeStep, velocityIterations, positionIterations);
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void show() {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment