?? robot.java
字號(hào):
import java.awt.*;
// 創(chuàng)建一個(gè)可以按照指定的方向移動(dòng)并可以使用武器開(kāi)火的簡(jiǎn)單機(jī)器人
public class Robot extends Actor2D
{
// 和當(dāng)前的動(dòng)畫(huà)關(guān)聯(lián)的索引
protected int currAnimIndex;
// 為設(shè)計(jì)動(dòng)畫(huà)保存前面的動(dòng)畫(huà)
protected int prevAnimIndex;
// Robot的開(kāi)火狀態(tài)
public final static int SHOOTING = 8;
// 用來(lái)告訴機(jī)器人朝哪個(gè)方向移動(dòng)
public final static int DIR_NORTH = 0;
public final static int DIR_SOUTH = 1;
public final static int DIR_EAST = 2;
public final static int DIR_WEST = 3;
// 用給定的角色組創(chuàng)建一個(gè)新的Robot
public Robot(ActorGroup2D grp)
{
super(grp);
vel.setX(5);
vel.setY(5);
animWait = 3;
currAnimIndex = 0;
prevAnimIndex = 0;
currAnimation = group.getAnimationStrip(RobotGroup.WALKING_SOUTH);
frameWidth = currAnimation.getFrameWidth();
frameHeight = currAnimation.getFrameHeight();
}
// 更新機(jī)器人的位置,如果它在射擊的話讓它動(dòng)起來(lái)
public void update()
{
if(hasState(SHOOTING))
{
animate();
}
xform.setToTranslation(pos.getX(), pos.getY());
updateBounds();
checkBounds();
}
// 讓機(jī)器人射擊直到stopShooting方法被調(diào)用
public void startShooting()
{
prevAnimIndex = currAnimIndex;
if((currAnimIndex % 2) == 0)
{
currAnimIndex++;
}
currAnimation = group.getAnimationStrip(currAnimIndex);
currAnimation.reset();
setState(SHOOTING);
}
// 停止射擊并回到射擊前的畫(huà)面
public void stopShooting()
{
currAnimIndex = prevAnimIndex;
currAnimation = group.getAnimationStrip(currAnimIndex);
currAnimation.reset();
resetState(SHOOTING);
}
// 根據(jù)指定的方向,讓機(jī)器人動(dòng)起來(lái)
public void move(int dir)
{
// 防止過(guò)多的射擊
resetState(SHOOTING);
switch(dir)
{
case DIR_NORTH:
if(currAnimIndex != RobotGroup.WALKING_NORTH)
{
prevAnimIndex = currAnimIndex;
currAnimation = group.getAnimationStrip(RobotGroup.WALKING_NORTH);
currAnimIndex = RobotGroup.WALKING_NORTH;
currAnimation.reset();
}
else
{
animate();
pos.translate(0, -vel.getY());
}
break;
case DIR_SOUTH:
if(currAnimIndex != RobotGroup.WALKING_SOUTH)
{
prevAnimIndex = currAnimIndex;
currAnimation = group.getAnimationStrip(RobotGroup.WALKING_SOUTH);
currAnimIndex = RobotGroup.WALKING_SOUTH;
currAnimation.reset();
}
else
{
animate();
pos.translate(0, vel.getY());
}
break;
case DIR_WEST:
if(currAnimIndex != RobotGroup.WALKING_WEST)
{
prevAnimIndex = currAnimIndex;
currAnimation = group.getAnimationStrip(RobotGroup.WALKING_WEST);
currAnimIndex = RobotGroup.WALKING_WEST;
currAnimation.reset();
}
else
{
animate();
pos.translate(-vel.getX(), 0);
}
break;
case DIR_EAST:
if(currAnimIndex != RobotGroup.WALKING_EAST)
{
prevAnimIndex = currAnimIndex;
currAnimation = group.getAnimationStrip(RobotGroup.WALKING_EAST);
currAnimIndex = RobotGroup.WALKING_EAST;
currAnimation.reset();
}
else
{
animate();
pos.translate(vel.getX(), 0);
}
break;
default:
break;
}
}
} // Robot
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -