gpt4 book ai didi

java - 如何在两个 CanvasView 对象上使用 onClickListener?

转载 作者:行者123 更新时间:2023-12-01 18:48:22 25 4
gpt4 key购买 nike

我正在尝试制作一个游戏,当用户单击射手时,它会改变其位置并进行射击,如果用户单击射出的球(或按项目中指定的方式射击),它们就会消失代码此处

射手.JAVA

public class shooter extends View {

//Consider all variables are declared
public shooter(int color, Context c) {
super(c);
paint = new Paint();
paint.setColor(color);
mContext = c;

}
public void move() {
//Moves Cannon On Click (CODE NOT SHOWN PURPOSELY)
invalidate();
}

public float getPosition()
{
return shootPoint;
}

public void draw(Canvas canvas)
{
super.draw(canvas);
// simply draws a rectangle (shooter)
cW=getWidth();
cH=getHeight();
center=new Point(cW/2,cH);
left=0; right=center.x; shootPoint=right/2;

canvas.drawRect(left,top,right,bottom,paint);
}
}

另一个名为shoot.java的Java类也可以在单击shootbutton时制作射击球但我希望当用户单击这些球(在 Canvas 上绘制)时,它们应该重置

主游戏 View 类


public class GameView extends FrameLayout implements View.OnClickListener {
//Consider all objects and variables are declared as used
public GameView(Context context, AttributeSet attrs) {
super(context,attrs);
//CONTAIN INITIALIZATION OF OBJECTS AS USED OF OTHER CLASSES
bullets = new ArrayList<shoot> ();
addView(cannon);
for (int i = 0; i < bullets.size(); i++ ) {
addView(bullets.get(i));
bullets.get(i).setOnClickListener(this);// an arrays of objects of shoot class
}
cannon.setOnClickListener(this);
level=3;level++;
}

@Override
public void onClick(View view) {
switch()// Here is the MAIN PROBLEM HOW SHOULD I DIFFERENTIATE THAT CANNON IS CLICKED OR //BULLETS LIKE USING VIEW.GETTAG()
{
case ----1:// WHAT CASE SSHOULD I WRITE
cannon.move();
break;

case ----2: // HERE ALSO
bullets.remove();
break;


}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawGameBoard(canvas);
try
{
Thread.sleep(5);
} catch (InterruptedException e) {
}



invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

width = w;
height = h;
aliens.setBounds(0,0,width,height);
for (int i = 0; i < bullets.size(); i++ ) {
bullets.get(i).setBounds(0,0,width,height);

}

}

public void drawGameBoard(Canvas canvas) {
cannon.draw(canvas);
for ( int i = bullets.size() - 1; i >= 0; i--) {
if (bullets.get(i) != null) {
bullets.get(i).draw(canvas);
}
}
for (int i = explosions.size() - 1; i >= 0; i--) {
if (explosions.get(i) != null) {
if (!explosions.get(i).draw(canvas)) {
explosions.remove(i);
}
}
}
if (aliens != null) {
aliens.draw(canvas);

RectF guyRect = aliens.getRect();

for (int i = bullets.size() - 1; i >= 0; i--) {
if (RectF.intersects(guyRect, bullets.get(i).getRect())) {
explosions.add(new explosion(Color.RED,mContext, aliens.getX()-aliens.dst, aliens.getY()-aliens.dst));
aliens.reset();
bullets.remove(i);
break;
}
}
if (!aliens.move()) {
aliens = null;
}
}
}
// Whenever the user shoots a bullet, create a new bullet moving upwards
public void shootCannon() {
bullets.add(new shoot(Color.RED, mContext, cannon.getPosition(), (float) (height-100)));

}
}

我已经通过注释展示了代码中遇到问题的部分,即GAMEVIEW.JAVA中的重写函数ONCLICK,例如如何知道单击了什么,以便执行其受尊重的函数如果您不明白我的问题,请通知我

最佳答案

如果我正确理解了这个问题,您希望使用一个 onClickListener 函数来处理您的大炮和子弹的点击事件。

由于两者是不同的类,您可以通过“instanceof”来区分它们。

这意味着你的 onClickListener 看起来像这样:

@Override
public void onClick(View view) {

if(view instanceof shooter) {
cannon.move();
}
else if(view instanceof shoot) {
bullets.remove();
}
}

希望这对您有帮助。如果有任何不清楚的地方,我很乐意回复:)

关于java - 如何在两个 CanvasView 对象上使用 onClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59776592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com