Android游戏开发中对象移动的例子
2010-05-14 16:47:00 来源:WEB开发网if( nowAnim != null ) {
nowAnim.stop();
// 必须要先stop才行,不然会影响到下一个动画的情况
nowAnim = null;
}
nowAnim = anim;
this.setBackgroundDrawable(nowAnim);
}
// 返回当前的动画,用于在其它地方控制停止等。
public AnimationDrawable getBgAnimationDrawable() {
return nowAnim;
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if( event.getAction() == KeyEvent.ACTION_DOWN )
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_LEFT: {
if( getLeft()>parent.getPaddingLeft() ) {
// 左或右位移量,是相对当前View的坐标
offsetLeftAndRight(-1*step);
// 记下当前的坐标值,用于在父view中重画经过的区域。
l -= step;
}
if( animLeft != nowAnim ) {
setAnimationDrawable( animLeft );
nowAnim.start();
}
break ;
}
case KeyEvent.KEYCODE_DPAD_RIGHT: {
if( getRight()<(parent.getWidth() - parent.getPaddingRight()) ) {
offsetLeftAndRight(step);
r += step;
}
if( animRight != nowAnim ) {
setAnimationDrawable( animRight );
nowAnim.start();
}
break ;
}
case KeyEvent.KEYCODE_DPAD_UP: {
if( getTop()>parent.getPaddingTop() ) {
offsetTopAndBottom(-1*step);
t -= step;
}
break ;
}
case KeyEvent.KEYCODE_DPAD_DOWN: {
if( getBottom()<(parent.getHeight() - parent.getPaddingBottom()) ) {
offsetTopAndBottom(step);
b += step;
}
break ;
}
case KeyEvent.KEYCODE_DPAD_CENTER: {
break ;
更多精彩
赞助商链接