我怎样才能让每只青蛙停止使用 onTouch?
How can I make each individual frog stop using onTouch?
我正在制作一个游戏,其中有青蛙在屏幕上跳来跳去。触摸青蛙后,我将游戏图像更改为我设置的 "deadFrog" 并且它停止移动。我将它们都创建在数组列表下,但我不确定如何只对单个青蛙进行更改。现在,如果轻敲一只青蛙,所有的青蛙都会停止移动并变成死蛙。希望你能帮我解决点击的战术核武器;) *如果你需要更多信息,请发表评论,我一定会提供!
编辑 没有办法访问块数组列表中的单个元素吗?例如,我尝试过执行 blocks(1),但那是无效的。
这里是声明青蛙的地方:
public void init() {
blocks = new ArrayList<Block>();
for (int i = 0; i < 5; i++) {
Block b = new Block(i * 200, MainActivity.GAME_HEIGHT - 95,
BLOCK_WIDTH, BLOCK_HEIGHT);
blocks.add(b);
tapped = false;
}
}
它们是这样渲染的:
private void renderFrogs(Painter g) {
if (!tapped) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if (b.isVisible()) {
Assets.runAnim.render(g, (int) b.getX(), (int) b.getY());
}
}
}
if (tapped) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if (b.isVisible()) {
g.drawImage(Assets.deadfrog, (int) b.getX(), (int) b.getY());
}
}
}
}
这是 onTouchListener:
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
recentTouchY = scaledY;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if ((scaledY >= b.getY() - BLOCK_HEIGHT || scaledY <= b.getY()) && (scaledX >= b.getX() || scaledX <= b.getX() + BLOCK_WIDTH)) {
tapped = true;
}
}
}
return true;
}
当然所有的青蛙都会死,你应该为每只青蛙保留 "tapped" 变量,你点击的变量一次适用于所有青蛙。
声明一个class
public class Frog extends View{
public Drawable liveFrog;
public Drawable deadFrog;
public boolean isDead;
public Point location;
public int width;
public int height;
public Frog(Context context, int x, int y,int width,int height){
super(context);
this.isDead = false;
this.location = new Point(x,y);
this.width = width;
this.height = height;
}
public void onDraw(Canvas c){
super.onDraw(c);
if(!isDead){
//draw live frog at x,y
}else {
//draw dead frog at x,y
}
}
}
那么你的数组应该包含青蛙
public void init(Context context) {
blocks = new ArrayList<Frog>();
for (int i = 0; i < 5; i++) {
Frog b = new Frog(context,i * 200, MainActivity.GAME_HEIGHT - 95,
BLOCK_WIDTH, BLOCK_HEIGHT);
blocks.add(b);
}
}
private void renderFrogs() {
for(Frog f : blocks){
//cause redraw
f.invalidate();
}
}
here comes the fun part, when you tap the frog
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
recentTouchY = scaledY;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < blocks.size(); i++) {
Frog frog = blocks.get(i);
if ((scaledY >= frog.getY() - BLOCK_HEIGHT || scaledY <= frog.getY()) && (scaledX >= frog.getX() || scaledX <= frog.getX() + BLOCK_WIDTH)) {
frog.isDead = true;
//cause one frog redraw
frog.invalidate();
//if the event was handled, stop here (unless you can have multiple frogs one on top of the other ?
return true;
}
}
}
//if the event was not handled, let it bubble up
return false;
}
据我所知,您的 "tapped" 布尔值不是 EACH 青蛙的 属性。它被声明一次,当被触发时,根据你的 for 循环,每只青蛙都会死去(很明显,因为这就是你正在经历的)。
一旦 "tapped" 为真,您的 for 循环将遍历每个块并为其分配一只死青蛙。
我认为您需要创建一个 Frog class,并将它们的实例存储在您的 ArrayList 中。新青蛙的一个变量 class 将是 "touched",当它被触发时,您将只对该特定实例做一些事情。
我正在制作一个游戏,其中有青蛙在屏幕上跳来跳去。触摸青蛙后,我将游戏图像更改为我设置的 "deadFrog" 并且它停止移动。我将它们都创建在数组列表下,但我不确定如何只对单个青蛙进行更改。现在,如果轻敲一只青蛙,所有的青蛙都会停止移动并变成死蛙。希望你能帮我解决点击的战术核武器;) *如果你需要更多信息,请发表评论,我一定会提供!
编辑 没有办法访问块数组列表中的单个元素吗?例如,我尝试过执行 blocks(1),但那是无效的。
这里是声明青蛙的地方:
public void init() {
blocks = new ArrayList<Block>();
for (int i = 0; i < 5; i++) {
Block b = new Block(i * 200, MainActivity.GAME_HEIGHT - 95,
BLOCK_WIDTH, BLOCK_HEIGHT);
blocks.add(b);
tapped = false;
}
}
它们是这样渲染的:
private void renderFrogs(Painter g) {
if (!tapped) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if (b.isVisible()) {
Assets.runAnim.render(g, (int) b.getX(), (int) b.getY());
}
}
}
if (tapped) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if (b.isVisible()) {
g.drawImage(Assets.deadfrog, (int) b.getX(), (int) b.getY());
}
}
}
}
这是 onTouchListener:
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
recentTouchY = scaledY;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);
if ((scaledY >= b.getY() - BLOCK_HEIGHT || scaledY <= b.getY()) && (scaledX >= b.getX() || scaledX <= b.getX() + BLOCK_WIDTH)) {
tapped = true;
}
}
}
return true;
}
当然所有的青蛙都会死,你应该为每只青蛙保留 "tapped" 变量,你点击的变量一次适用于所有青蛙。
声明一个class
public class Frog extends View{
public Drawable liveFrog;
public Drawable deadFrog;
public boolean isDead;
public Point location;
public int width;
public int height;
public Frog(Context context, int x, int y,int width,int height){
super(context);
this.isDead = false;
this.location = new Point(x,y);
this.width = width;
this.height = height;
}
public void onDraw(Canvas c){
super.onDraw(c);
if(!isDead){
//draw live frog at x,y
}else {
//draw dead frog at x,y
}
}
}
那么你的数组应该包含青蛙
public void init(Context context) {
blocks = new ArrayList<Frog>();
for (int i = 0; i < 5; i++) {
Frog b = new Frog(context,i * 200, MainActivity.GAME_HEIGHT - 95,
BLOCK_WIDTH, BLOCK_HEIGHT);
blocks.add(b);
}
}
private void renderFrogs() {
for(Frog f : blocks){
//cause redraw
f.invalidate();
}
}
here comes the fun part, when you tap the frog
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
recentTouchY = scaledY;
} else if (e.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < blocks.size(); i++) {
Frog frog = blocks.get(i);
if ((scaledY >= frog.getY() - BLOCK_HEIGHT || scaledY <= frog.getY()) && (scaledX >= frog.getX() || scaledX <= frog.getX() + BLOCK_WIDTH)) {
frog.isDead = true;
//cause one frog redraw
frog.invalidate();
//if the event was handled, stop here (unless you can have multiple frogs one on top of the other ?
return true;
}
}
}
//if the event was not handled, let it bubble up
return false;
}
据我所知,您的 "tapped" 布尔值不是 EACH 青蛙的 属性。它被声明一次,当被触发时,根据你的 for 循环,每只青蛙都会死去(很明显,因为这就是你正在经历的)。
一旦 "tapped" 为真,您的 for 循环将遍历每个块并为其分配一只死青蛙。
我认为您需要创建一个 Frog class,并将它们的实例存储在您的 ArrayList 中。新青蛙的一个变量 class 将是 "touched",当它被触发时,您将只对该特定实例做一些事情。