在屏幕上移动矩形

时间:2016-01-13 01:39:14

标签: java android

我有一个矩形,我想以恒定的速度向上移动屏幕,并且对于为什么我的代码不起作用感到困惑。以下是代码:

package com.ashmore.MyGame;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import com.ashmore.framework.Game;
import com.ashmore.framework.Graphics;
import com.ashmore.framework.Image;
import com.ashmore.framework.Screen;
import com.ashmore.framework.Input.TouchEvent;

public class GameScreen extends Screen {
enum GameState {
Ready, Running, Paused
}

GameState state = GameState.Ready;

boolean BarisMoving = false;

private ArrayList<Rect> rectangles = new ArrayList<Rect>();

int bar_x = 32;
int bar_y = 653;
int bar_width = 183;
int bar_height = 648;

Rect bar;
Paint paint;

public GameScreen(Game game) {
super(game);

// Initialize game objects here

bar = new Rect();
bar.set(bar_x, bar_y, bar_width, bar_height);

// Defining a paint object
paint = new Paint();
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);

}

private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {

// 1. All touch input is handled here:
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);

BarisMoving = true;

if (event.type == TouchEvent.TOUCH_UP) {

if (ScalesScreen.scaleType.equals("C")) {
rectangles.add(new Rect(56, 400, 80, 435));
//rectangles.get(0);
break;  
}
if (BarisMoving) {

 bar_y = bar_y -= 10;


 }
 for (Rect rect : rectangles) {
if(bar.intersect(rect)) {

checkButtons();

 }

 }

 }

}

private void checkButtons() {
  Log.d("GameScreen","Note and Bar Intersected");
}

@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();

Paint paint = new Paint();
Paint paint2 = new Paint();

paint.setColor(Color.RED);
paint2.setColor(Color.RED);

for (Rect rect : rectangles) {
g.drawRect(rect, paint);
}

g.drawRect(bar, paint2);

}

private boolean inBounds(TouchEvent event, int x, int y, int width,
        int height) {
    if (event.x > x && event.x < x + width - 1 && event.y > y
            && event.y < y + height - 1)
        return true;
    else
        return false;
}
}

我可能遗漏了一些非常基本的东西。但是,我似乎无法找到问题。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了我的问题。问题是我的代码放在updateRunning()方法中。在我的问题中,布尔值BarisMoving的设置被设置为等于&#34; true&#34;当有触摸事件时:

int len = touchEvents.size();
for (int i = 0; i < len; i++) {

TouchEvent event = touchEvents.get(i);

BarisMoving = true;

布尔值必须移到此区域之外(在int len = touchEvents.size();上方)并且在整个触摸事件区域之外以我想要的方式工作(在for(){和结束括号之外) })。