Xác định vị trí con trỏTrong các trò chơi 2d điển hình như mario nổi tiếng từ rất lâu thì việc cực kỳ quan trọng của người lập trình là phải xác định vị trí của nhân vật.. Trong bài viết
Trang 1Xác định vị trí con trỏ
Trong các trò chơi 2d (điển hình như mario) nổi tiếng từ rất lâu thì việc cực kỳ quan trọng của người lập trình là phải xác định vị trí của nhân vật Trong bài viết này mình sẽ tạo 1 ứng dụng xác định vị trí khi bạn nhấn các button lên,xuống,trái,phải Ứng dụng này giả sử bạn đang điều khiển con vua ở trong bàn cờ vua, bạn đang ở vị trí chính giữa bàn cờ Mặc định vị trí của bạn là center (chính giữa), và khi bạn bấm các button điều khiển thì con vua sẽ di chuyển theo sự điều khiển của bạn.
Đầu tiên bạn tạo 1 Project như sau:
Project name: ContentSlider
Build Target: Android 2.3.3
Application name: ContentSlider
Activity: ContentSlider
Package name: com.paad.contentslider
Tiếp theo vbạn tao folder anim trong folder res Folder này giữ các file XML xác định cách thức di chuyển của bạn Khi đã tạo xong, bạn tạo tiếp 8 file XML đại diện cho 8 ô
di chuyển của bạn.
+ slide_bottom_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0"
android:duration="700"
/>
</set>
+ slide_bottom_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromYDelta="0"
android:toYDelta="100%p"
android:duration="700"
/>
</set>
Trang 2+ slide_left_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> <translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="700"
/>
</set>
+ slide_left_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> <translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="700"
/>
</set>
+ slide_right_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> <translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="700"
/>
</set>
+ slide_right_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> <translate
Trang 3android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="700"
/>
</set>
+ slide_top_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> <translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="700"
/>
</set>
+ slide_top_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"> <translate
android:fromYDelta="0"
android:toYDelta="-100%p"
android:duration="700"
/>
</set>
Và trong file Activity chính bạn viết code xử lý như sau:
package com.paad.contentslider;
import android.app.Activity;
import android.view.KeyEvent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
Trang 4import android.widget.TextView;
public class ContentSlider extends Activity {
TextPosition textPosition = TextPosition.Center;
enum TextPosition { UpperLeft, Top, UpperRight,
Left, Center, Right,
LowerLeft, Bottom, LowerRight };
Animation slideInLeft;
Animation slideOutLeft;
Animation slideInRight;
Animation slideOutRight;
Animation slideInTop;
Animation slideOutTop;
Animation slideInBottom;
Animation slideOutBottom;
TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left_in); slideOutLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left_out); slideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_right_in); slideOutRight = AnimationUtils.loadAnimation(this, R.anim.slide_right_out); slideInTop = AnimationUtils.loadAnimation(this, R.anim.slide_top_in); slideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_top_out); slideInBottom = AnimationUtils.loadAnimation(this, R.anim.slide_bottom_in); slideOutBottom = AnimationUtils.loadAnimation(this, R.anim.slide_bottom_out); myTextView = (TextView)findViewById(R.id.myTextView);
}
private void applyAnimation(Animation _out, Animation _in, String _newText) { final String text = _newText;
final Animation in = _in;
// Ensure the text stays out of screen when the slide-out
// animation has completed
_out.setFillAfter(true);
// Create a listener to wait for the slide-out
// animation to complete
_out.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation _animation) {
// Change the text
myTextView.setText(text);
// Slide it back in to view
myTextView.startAnimation(in);
Trang 5}
public void onAnimationRepeat(Animation _animation) {}
public void onAnimationStart(Animation _animation) {}
});
// Apply the slide-out animation
myTextView.startAnimation(_out);
}
private void movePosition(TextPosition _current, TextPosition _directionPressed) { Animation in;
Animation out;
TextPosition newPosition;
if (_directionPressed == TextPosition.Left){
in = slideInLeft;
out = slideOutLeft;
}
else if (_directionPressed == TextPosition.Right){
in = slideInRight;
out = slideOutRight;
}
else if (_directionPressed == TextPosition.Top){
in = slideInTop;
out = slideOutTop;
}
else {
in = slideInBottom;
out = slideOutBottom;
}
int newPosValue = _current.ordinal();
int currentValue = _current.ordinal();
// To simulate the effect of 'tilting' the device moving in one
// direction should make text for the opposite direction appear
// Ie Tilting right should make left appear
if (_directionPressed == TextPosition.Bottom)
newPosValue = currentValue - 3;
else if (_directionPressed == TextPosition.Top)
newPosValue = currentValue + 3;
else if (_directionPressed == TextPosition.Right) {
if (currentValue % 3 != 0)
newPosValue = currentValue - 1;
}
else if (_directionPressed == TextPosition.Left) {
if ((currentValue+1) % 3 != 0)
newPosValue = currentValue + 1;
}
Trang 6if (newPosValue != currentValue &&
newPosValue > -1 &&
newPosValue < 9){
newPosition = TextPosition.values()[newPosValue];
applyAnimation(in, out, newPosition.toString());
textPosition = newPosition;
}
}
@Override
public boolean onKeyDown(int _keyCode, KeyEvent _event) {
if (super.onKeyDown(_keyCode, _event))
return true;
if (_event.getAction() == KeyEvent.ACTION_DOWN){
switch (_keyCode) {
case (KeyEvent.KEYCODE_DPAD_LEFT):
movePosition(textPosition, TextPosition.Left); return true; case (KeyEvent.KEYCODE_DPAD_RIGHT):
movePosition(textPosition, TextPosition.Right); return true; case (KeyEvent.KEYCODE_DPAD_UP):
movePosition(textPosition, TextPosition.Top); return true; case (KeyEvent.KEYCODE_DPAD_DOWN):
movePosition(textPosition, TextPosition.Bottom);
return true;
}
}
return false;
}
}
Cuối cùng bạn debug ứng dụng và có kể quả như sau:
Trang 7Khi bạn bấm phím bên phải (vị trí cũ sẽ nằm ở bên trái) thì sẽ được như sau:
Trang 8Khi bạn bấm nút lên (vị trí cũ nam ở dưới bên trái thi sẽ đc như sau):
Trang 9Mọi ý kiến đóng góp các bạn vui lòng gữi bài viết về forum www.laptrinhdidong.vn Rất mong nhận được phản hồi của các bạn.