view_keypad.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="bottom"
android:padding="@dimen/keypad_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/keypad">
<Button android:id="@+id/keypad_up"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/keypad_left"
android:layout_width="@dimen/keypad_size"
android:layout_height="@dimen/keypad_size" />
<Button android:id="@+id/keypad_down"
android:layout_below="@+id/keypad_left"
android:layout_toRightOf="@+id/keypad_left"
android:layout_width="@dimen/keypad_size"
android:layout_height="@dimen/keypad_size" />
<Button android:id="@+id/keypad_left"
android:layout_alignParentLeft="true"
android:layout_below="@+id/keypad_up"
android:layout_width="@dimen/keypad_size"
android:layout_height="@dimen/keypad_size" />
<Button android:id="@+id/keypad_right"
android:layout_toRightOf="@+id/keypad_up"
android:layout_below="@+id/keypad_up"
android:layout_width="@dimen/keypad_size"
android:layout_height="@dimen/keypad_size" />
<Button android:id="@+id/keypad_jump"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/keypad_left"
android:layout_width="@dimen/keypad_size"
android:layout_height="@dimen/keypad_size" />
</RelativeLayout>
//update styles.xml with fullscreen-flags
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:immersive">true</item>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ulfben.platformer.MainActivity">
<view class="com.ulfben.platformer.GameView"
android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<include layout="@layout/view_keypad" />
</FrameLayout>
//constructors needed for view-inflation to not crash horribly
public GameView(Context context) {
super(context);
init(context);
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
//BasicInputManager.java
BasicInputManager(View view){
view.findViewById(R.id.keypad_up).setOnTouchListener(this);
view.findViewById(R.id.keypad_down).setOnTouchListener(this);
view.findViewById(R.id.keypad_left).setOnTouchListener(this);
view.findViewById(R.id.keypad_right).setOnTouchListener(this);
view.findViewById(R.id.keypad_jump).setOnTouchListener(this);
}
@Override
public boolean onTouch(final View v, final MotionEvent event) {
int action = event.getActionMasked();
int id = v.getId();
if(action == MotionEvent.ACTION_DOWN){// User started pressing a key
if(id == R.id.keypad_up){
mVerticalFactor -= 1;
}else if (id == R.id.keypad_down) {
mVerticalFactor += 1;
}
if (id == R.id.keypad_left) {
mHorizontalFactor -= 1;
} else if(id == R.id.keypad_right) {
mHorizontalFactor += 1;
}
if (id == R.id.keypad_jump) {
mIsJumping = true;
}
} else if(action == MotionEvent.ACTION_UP) {
if (id == R.id.keypad_up) {
mVerticalFactor += 1;
} else if (id == R.id.keypad_down) {
mVerticalFactor -= 1;
}
if (id == R.id.keypad_left) {
mHorizontalFactor += 1;
} else if (id == R.id.keypad_right) {
mHorizontalFactor -= 1;
}
if (id == R.id.keypad_jump) {
mIsJumping = false;
}
}
return false;
}
Comments
0 B
|0 👍
/0 👎
0 B
|👍
/👎