My first android game is not responding to touch input - java

I am creating a very simple game, because I am new to developing on the android platform, but not programming, in general. Anyway, when I run the app via the emulator, or sometimes through an actual device, all I see are the two paddles being drawn, including ball, on the screen, and when I touch the screen, or simulate touching with a mouse, the event onTouch, doesn't get fired. I do not know why, can anyone point me to the right direction, I am new to developing on android devices.
Code:
package main.game;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.*;
public class DrawingArea extends SurfaceView
{
private Ball ball = null;
private Paddle player = null;
private Paddle computer = null;
private Vector2 playerPos = Vector2.Zero;
private boolean startGame = false;
private boolean initiated = false;
private Paint paint;
public DrawingArea(Context context, Paint paint) {
super(context);
setOnTouchListener(new Touch());
setWillNotDraw(false);
this.paint = paint;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
if (initiated == false)
{
ball = new Ball(getWidth() / 2.0f, getHeight() / 2.0f, 32, paint);
player = new Paddle(playerPos.getX(), playerPos.getY(), 32, 128, paint);
Paint p = new Paint();
p.set(paint);
p.setColor(Color.GREEN);
computer = new Paddle(getWidth() - 32, 0, 32, 128, p);
GameLoop gloop = new GameLoop(this);
gloop.start();
UpdateLoop uloop = new UpdateLoop(this);
uloop.start();
initiated = true;
}
ball.draw(canvas);
player.draw(canvas);
computer.draw(canvas);
}
class GameLoop extends Thread
{
private DrawingArea area;
private boolean running = true;
public GameLoop(DrawingArea area)
{
this.area = area;
}
#Override
public void run() {
int frame = 0;
while(running)
{
this.area.invalidate();
if(frame == Integer.MAX_VALUE) { frame = 0;}
frame++;
}
}
public void closeThread()
{
running = false;
}
}
class Touch implements View.OnTouchListener
{
#Override
public boolean onTouch(View v, MotionEvent event) {
startGame = true;
playerPos = new Vector2(0, event.getY());
if (playerPos.getY() < (player.getY() + 256))
{
player.translateY(-0.5f);
}
else if (playerPos.getY() > (player.getY() + 256))
{
player.translateY(0.5f);
}
return true;
}
}
class UpdateLoop extends Thread
{
private DrawingArea area;
private boolean running = true;
private boolean forward = true;
public UpdateLoop(DrawingArea area)
{
this.area = area;
}
#Override
public void run() {
int frame = 0;
while(running)
{
if (startGame == true)
ball.translate(0.5f, 0.5f, 45.0f, forward);
if (ball.getX() < 0 || ball.getY() < 0 || ball.getX() > area.getWidth()|| ball.getY() > area.getHeight()|| ball.Intersects(computer) || ball.Intersects(player))
{
if(forward == true)
forward = false;
else
forward = true;
}
if(frame == Integer.MAX_VALUE) { frame = 0;}
frame++;
}
}
}
}
Update:
DrawingArea area = new DrawingArea(this, paint);
setContentView(R.layout.main);
LinearLayout main = (LinearLayout)findViewById(R.id.main);
main.addView(area, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
main.setOnTouchListener(this);

Instead of registering your listener to surfaceview
setOnTouchListener(new Touch());
Try attaching the listener to the layout for the view something like this
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
//the whole screen becomes sensitive to touch
mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
mLinearLayoutMain.setOnTouchListener(this);
}

Related

Moving two objects in the same class on a JFrame in Java

I have a project I am working on in that I have to create two objects, I would like the big rectangle(BLUE) to move around the frame every time I press the arrow keys on my keyboard while the small rectangle(RED) is moving away, once the big square touches/tags the small rectangle, screen refreshes and I can move the big rectangle again to chase down the small rectangle. Below is my main class, and IT class where I have implemented my two shapes.
The goal is to have the two rectangles, small rectangle runs away in the frame every time the big rectangle comes close until it's tagged. Would also have to add some kind of score panel on the frame to show updated scores and a timer to count down when the player starts playing.
I need help having the two rectangles move differently and not on top of each other. I would like the second rectangle to move away every time the first rectangle comes close to it moving around the Frame
`
My class IT
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class IT extends JPanel implements ActionListener, KeyListener {
Timer shapeTimer = new Timer(5, this);
public double xPos = 0, yPos = 0, movementX = 0, movementY = 0;
public int rectSize = 50;
public int rectSize2 = 35;
public int windowWidth;
int windowHeight;
public int xBound;
public int yBound;
public IT(int w, int h){
shapeTimer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
windowWidth = w;
windowHeight = h;
xBound = (windowWidth - rectSize);
yBound = (windowHeight - rectSize);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D movableRect = new Rectangle2D.Double(xPos, yPos, rectSize, rectSize);
g2.setColor(Color.BLUE);
g2.draw(movableRect);
g2.fill(movableRect);
Rectangle2D movableRect2 = new Rectangle2D.Double(xPos, yPos, rectSize2, rectSize2);
g2.setColor(Color.RED);
g2.draw(movableRect2);
g2.fill(movableRect2);
}
public void actionPerformed(ActionEvent e){
repaint();
xPos += movementX;
yPos += movementY;
}
public void moveUp(){
if (yPos == 0){
movementY = 0;
movementX = 0;
}
movementY = -0.5;
movementX = 0;
}
public void moveDown(){
if (yPos == yBound){
movementY = 0;
movementX = 0;
}
movementY = 0.5;
movementX = 0;
}
public void moveLeft()
{
if (xPos == 0){
movementY = 0;
movementX = 0;
}
movementX = -0.5;
movementY = 0;
}
public void moveRight(){
if (xPos == xBound)
{
movementY = 0;
movementX = 0;
}
movementX = 0.5;
movementY = 0;
}
public void enlargeSquare(){
rectSize++;
rectSize2++;
}
public void shrinkSquare(){
rectSize--;
rectSize2--;
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP){
moveUp();
}
if (keyCode == KeyEvent.VK_DOWN){
moveDown();
}
if (keyCode == KeyEvent.VK_RIGHT){
moveRight();
}
if (keyCode == KeyEvent.VK_LEFT){
moveLeft();
}
if (keyCode == KeyEvent.VK_OPEN_BRACKET)
{
shrinkSquare();
}
if (keyCode == KeyEvent.VK_CLOSE_BRACKET)
{
enlargeSquare();
}
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP){
movementX = 0;
movementY = 0;
}
if (keyCode == KeyEvent.VK_DOWN){
movementX = 0;
movementY = 0;
}
if (keyCode == KeyEvent.VK_RIGHT){
movementX = 0;
movementY = 0;
}
if (keyCode == KeyEvent.VK_UP){
movementX = 0;
movementY = 0;
}
}
}
`
MainTester class
`
import javax.swing.*;
import javax.swing.JFrame;
public class MainTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
int frameWidth = 850;
int frameHeight = 650;
JFrame frmMain = new JFrame();
frmMain.setSize(frameWidth, frameHeight);
IT it = new IT(frameWidth, frameHeight);
frmMain.add(it);
frmMain.setVisible(true);
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMain.setTitle("Tag Game");
}
}
`
Use key bindings, seriously, this is going to solve a swagger of issues related to KeyListener.
Decouple and seperate your logic. Each "entity" should be a self contained unit of work. In your case, they should contain information about their color, location and size at a minimum.
For simplicity, I started out with something which could just be painted...
public interface Entity {
public void paint(Graphics2D g2d);
}
Now, you could have a lot of different interfaces which reflect this which can be painted, moved, controlled, represents effects or what ever you need - then your classes should implement the interfaces they need. Then, your engine just deals with the "concepts" it wants to - need all "paintable" entities, need all "movable" entities, etc, when it needs to.
Next I created a concept of a "player". A player is a "paintable" entity, but which can be controlled by the player in some way, so it takes the current state of the "input"s and updates itself based on those states (more about that to come)
public static class PlayerEntity implements Entity {
protected static final int DELTA = 2;
private Rectangle bounds = new Rectangle(0, 0, 35, 35);
private Color fillColor;
public PlayerEntity(Color fillColor, Point location) {
this.fillColor = fillColor;
this.bounds.setLocation(location);
}
public Color getFillColor() {
return fillColor;
}
public Rectangle getBounds() {
return bounds;
}
public Point getCenter() {
return new Point((int)getBounds().getCenterX(), (int)getBounds().getCenterY());
}
public void update(Set<PlayerAction> actions, Dimension size) {
Rectangle currentBounds = getBounds();
int x = currentBounds.x;
int y = currentBounds.y;
if (actions.contains(PlayerAction.UP)) {
y -= DELTA;
}
if (actions.contains(PlayerAction.DOWN)) {
y += DELTA;
}
if (actions.contains(PlayerAction.LEFT)) {
x -= DELTA;
}
if (actions.contains(PlayerAction.RIGHT)) {
x += DELTA;
}
if (y < 0) {
y = 0;
}
if (y + currentBounds.height > size.height) {
y = size.height - currentBounds.height;
}
if (x < 0) {
x = 0;
}
if (x + currentBounds.width > size.width) {
x = size.width - currentBounds.width;
}
getBounds().setLocation(x, y);
}
#Override
public void paint(Graphics2D g2d) {
g2d.setColor(getFillColor());
g2d.fill(getBounds());
}
}
For reference, PlayerAction represents all the valid actions which can be performed by the player, for simplicity sake, I've just stuck to movement:
public enum PlayerAction {
UP, DOWN, LEFT, RIGHT;
}
Next I created a "monster" entity, in this case, the "monster" will always try and follow the player, this implementation is loosely based on Java: Move image towards mouse position
public static class MonsterEntity implements Entity {
protected static final int DELTA = 1;
private Rectangle bounds = new Rectangle(0, 0, 15, 15);
private Color fillColor;
public MonsterEntity(Color fillColor, Point location) {
this.fillColor = fillColor;
this.bounds.setLocation(location);
}
public Color getFillColor() {
return fillColor;
}
public Rectangle getBounds() {
return bounds;
}
public Point getCenter() {
return new Point((int)getBounds().getCenterX(), (int)getBounds().getCenterY());
}
public void moveTowards(Point target) {
Rectangle bounds = getBounds();
Point center = getCenter();
int xDelta = target.x < center.x ? -DELTA : DELTA;
int yDelta = target.y < center.y ? -DELTA : DELTA;
getBounds().setLocation(bounds.x + xDelta, bounds.y + yDelta);
}
#Override
public void paint(Graphics2D g2d) {
g2d.setColor(getFillColor());
g2d.fill(getBounds());
}
}
The monster is always trying to move it's center to the target location (which will eventually be the center of the player)
Now, this is where things become complicated.
We need:
A renderable surface onto which we can paint the player and monster(s)
Input bindings
A "game loop" to update the state of the entities and schedule repaints
For simplicity, I started with a JPanel, made use of Swing Timer and the key bindings API.
public class MainPane extends JPanel {
// This represents the "input bindings", these represent
// abstract actions which can be applied to the player
// or game state.
private enum InputKey {
PRESSED_UP, PRESSED_DOWN, PRESSED_LEFT, PRESSED_RIGHT,
RELEASED_UP, RELEASED_DOWN, RELEASED_LEFT, RELEASED_RIGHT;
public KeyStroke getKeyStroke() {
switch (this) {
case PRESSED_UP: return KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false);
case PRESSED_DOWN: return KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false);
case PRESSED_LEFT: return KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false);
case PRESSED_RIGHT: return KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false);
case RELEASED_UP: return KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);
case RELEASED_DOWN: return KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true);
case RELEASED_LEFT: return KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);
case RELEASED_RIGHT: return KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);
}
return null;
}
}
private PlayerEntity playerEntity;
private MonsterEntity monsterEntity;
private Timer timer;
private Set<PlayerAction> actions = new HashSet<PlayerAction>();
public MainPane() {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(InputKey.PRESSED_UP.getKeyStroke(), InputKey.PRESSED_UP);
inputMap.put(InputKey.PRESSED_DOWN.getKeyStroke(), InputKey.PRESSED_DOWN);
inputMap.put(InputKey.PRESSED_LEFT.getKeyStroke(), InputKey.PRESSED_LEFT);
inputMap.put(InputKey.PRESSED_RIGHT.getKeyStroke(), InputKey.PRESSED_RIGHT);
inputMap.put(InputKey.RELEASED_UP.getKeyStroke(), InputKey.RELEASED_UP);
inputMap.put(InputKey.RELEASED_DOWN.getKeyStroke(), InputKey.RELEASED_DOWN);
inputMap.put(InputKey.RELEASED_LEFT.getKeyStroke(), InputKey.RELEASED_LEFT);
inputMap.put(InputKey.RELEASED_RIGHT.getKeyStroke(), InputKey.RELEASED_RIGHT);
actionMap.put(InputKey.PRESSED_UP, new MoveAction(actions, PlayerAction.UP, true));
actionMap.put(InputKey.PRESSED_DOWN, new MoveAction(actions, PlayerAction.DOWN, true));
actionMap.put(InputKey.PRESSED_LEFT, new MoveAction(actions, PlayerAction.LEFT, true));
actionMap.put(InputKey.PRESSED_RIGHT, new MoveAction(actions, PlayerAction.RIGHT, true));
actionMap.put(InputKey.RELEASED_UP, new MoveAction(actions, PlayerAction.UP, false));
actionMap.put(InputKey.RELEASED_DOWN, new MoveAction(actions, PlayerAction.DOWN, false));
actionMap.put(InputKey.RELEASED_LEFT, new MoveAction(actions, PlayerAction.LEFT, false));
actionMap.put(InputKey.RELEASED_RIGHT, new MoveAction(actions, PlayerAction.RIGHT, false));
Dimension size = getPreferredSize();
Point center = new Point((size.width - 35) / 2, (size.height - 35) / 2);
playerEntity = new PlayerEntity(Color.BLUE, center);
monsterEntity = new MonsterEntity(Color.RED, new Point(size.width - 15, size.height - 15));
}
#Override
public void addNotify() {
super.addNotify();
if (timer != null) {
timer.stop();
}
timer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
performTick();
}
});
timer.start();
}
#Override
public void removeNotify() {
super.removeNotify();
if (timer != null) {
timer.stop();
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
protected void performTick() {
playerEntity.update(actions, getSize());
monsterEntity.moveTowards(playerEntity.getCenter());
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
playerEntity.paint(g2d);
monsterEntity.paint(g2d);
g2d.dispose();
}
}
Movement (or input) is controlled through the key bindings API, this triggers a MoveAction which updates a centralised state repository (which is then passed to the PlayerEntity so it apply the state accordingly).
For simplicity, I've only used a single Action, but you could make a couple, one representing "press/activate" or "release/deactivate"
public class MoveAction extends AbstractAction {
private Set<PlayerAction> actions;
private PlayerAction action;
private boolean activate;
public MoveAction(Set<PlayerAction> directions, PlayerAction direction, boolean activate) {
this.actions = directions;
this.action = direction;
this.activate = activate;
}
#Override
public void actionPerformed(ActionEvent e) {
if (activate) {
actions.add(action);
} else {
actions.remove(action);
}
}
}
See How to Use Actions for more details about actions.
But why follow this workflow?!
It decouples and decentralises a lot of the workflows. In fact, if you really wanted to, you could also seperate out the Timer and "paint" workflows to seperate classes, further decoupling the classes.
Key bindings solve all the issues related to KeyListener weirdness. It also decouples the input - want to add touch controls/buttons, no worries, it's done via Actions. Want to add joystick/controllers, no worries, it's done via Actions.
Want more monsters?
Change:
private MonsterEntity monsterEntity;
to:
private List<MonsterEntity> monsterEntitys = new ArrayList<>(32);
Change:
monsterEntity = new MonsterEntity(Color.RED, new Point(size.width - 15, size.height - 15));
to:
monsterEntitys.add(new MonsterEntity(Color.RED, new Point(size.width - 15, size.height - 15)));
monsterEntitys.add(new MonsterEntity(Color.RED, new Point(0, 0)));
monsterEntitys.add(new MonsterEntity(Color.RED, new Point(size.width - 15, 0)));
monsterEntitys.add(new MonsterEntity(Color.RED, new Point(0, size.height - 15)));
Change:
monsterEntity.paint(g2d);
to:
for (MonsterEntity entity : monsterEntitys) {
entity.paint(g2d);
}
And now you have more monsters! Have fun with that!
Runnable example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public enum PlayerAction {
UP, DOWN, LEFT, RIGHT;
}
public class MainPane extends JPanel {
private enum InputKey {
PRESSED_UP, PRESSED_DOWN, PRESSED_LEFT, PRESSED_RIGHT,
RELEASED_UP, RELEASED_DOWN, RELEASED_LEFT, RELEASED_RIGHT;
public KeyStroke getKeyStroke() {
switch (this) {
case PRESSED_UP: return KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false);
case PRESSED_DOWN: return KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false);
case PRESSED_LEFT: return KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false);
case PRESSED_RIGHT: return KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false);
case RELEASED_UP: return KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);
case RELEASED_DOWN: return KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true);
case RELEASED_LEFT: return KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);
case RELEASED_RIGHT: return KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);
}
return null;
}
}
private PlayerEntity playerEntity;
private MonsterEntity monsterEntity;
private Timer timer;
private Set<PlayerAction> actions = new HashSet<PlayerAction>();
public MainPane() {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(InputKey.PRESSED_UP.getKeyStroke(), InputKey.PRESSED_UP);
inputMap.put(InputKey.PRESSED_DOWN.getKeyStroke(), InputKey.PRESSED_DOWN);
inputMap.put(InputKey.PRESSED_LEFT.getKeyStroke(), InputKey.PRESSED_LEFT);
inputMap.put(InputKey.PRESSED_RIGHT.getKeyStroke(), InputKey.PRESSED_RIGHT);
inputMap.put(InputKey.RELEASED_UP.getKeyStroke(), InputKey.RELEASED_UP);
inputMap.put(InputKey.RELEASED_DOWN.getKeyStroke(), InputKey.RELEASED_DOWN);
inputMap.put(InputKey.RELEASED_LEFT.getKeyStroke(), InputKey.RELEASED_LEFT);
inputMap.put(InputKey.RELEASED_RIGHT.getKeyStroke(), InputKey.RELEASED_RIGHT);
actionMap.put(InputKey.PRESSED_UP, new MoveAction(actions, PlayerAction.UP, true));
actionMap.put(InputKey.PRESSED_DOWN, new MoveAction(actions, PlayerAction.DOWN, true));
actionMap.put(InputKey.PRESSED_LEFT, new MoveAction(actions, PlayerAction.LEFT, true));
actionMap.put(InputKey.PRESSED_RIGHT, new MoveAction(actions, PlayerAction.RIGHT, true));
actionMap.put(InputKey.RELEASED_UP, new MoveAction(actions, PlayerAction.UP, false));
actionMap.put(InputKey.RELEASED_DOWN, new MoveAction(actions, PlayerAction.DOWN, false));
actionMap.put(InputKey.RELEASED_LEFT, new MoveAction(actions, PlayerAction.LEFT, false));
actionMap.put(InputKey.RELEASED_RIGHT, new MoveAction(actions, PlayerAction.RIGHT, false));
Dimension size = getPreferredSize();
Point center = new Point((size.width - 35) / 2, (size.height - 35) / 2);
playerEntity = new PlayerEntity(Color.BLUE, center);
monsterEntity = new MonsterEntity(Color.RED, new Point(size.width - 15, size.height - 15));
}
#Override
public void addNotify() {
super.addNotify();
if (timer != null) {
timer.stop();
}
timer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
performTick();
}
});
timer.start();
}
#Override
public void removeNotify() {
super.removeNotify();
if (timer != null) {
timer.stop();
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
protected void performTick() {
playerEntity.update(actions, getSize());
monsterEntity.moveTowards(playerEntity.getCenter());
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
playerEntity.paint(g2d);
monsterEntity.paint(g2d);
g2d.dispose();
}
}
public class MoveAction extends AbstractAction {
private Set<PlayerAction> actions;
private PlayerAction action;
private boolean activate;
public MoveAction(Set<PlayerAction> directions, PlayerAction direction, boolean activate) {
this.actions = directions;
this.action = direction;
this.activate = activate;
}
#Override
public void actionPerformed(ActionEvent e) {
if (activate) {
actions.add(action);
} else {
actions.remove(action);
}
}
}
public interface Entity {
public void paint(Graphics2D g2d);
}
public static class MonsterEntity implements Entity {
protected static final int DELTA = 1;
private Rectangle bounds = new Rectangle(0, 0, 15, 15);
private Color fillColor;
public MonsterEntity(Color fillColor, Point location) {
this.fillColor = fillColor;
this.bounds.setLocation(location);
}
public Color getFillColor() {
return fillColor;
}
public Rectangle getBounds() {
return bounds;
}
public Point getCenter() {
return new Point((int)getBounds().getCenterX(), (int)getBounds().getCenterY());
}
public void moveTowards(Point target) {
Rectangle bounds = getBounds();
Point center = getCenter();
int xDelta = target.x < center.x ? -DELTA : DELTA;
int yDelta = target.y < center.y ? -DELTA : DELTA;
getBounds().setLocation(bounds.x + xDelta, bounds.y + yDelta);
}
#Override
public void paint(Graphics2D g2d) {
g2d.setColor(getFillColor());
g2d.fill(getBounds());
}
}
public static class PlayerEntity implements Entity {
protected static final int DELTA = 2;
private Rectangle bounds = new Rectangle(0, 0, 35, 35);
private Color fillColor;
public PlayerEntity(Color fillColor, Point location) {
this.fillColor = fillColor;
this.bounds.setLocation(location);
}
public Color getFillColor() {
return fillColor;
}
public Rectangle getBounds() {
return bounds;
}
public Point getCenter() {
return new Point((int)getBounds().getCenterX(), (int)getBounds().getCenterY());
}
public void update(Set<PlayerAction> actions, Dimension size) {
Rectangle currentBounds = getBounds();
int x = currentBounds.x;
int y = currentBounds.y;
if (actions.contains(PlayerAction.UP)) {
y -= DELTA;
}
if (actions.contains(PlayerAction.DOWN)) {
y += DELTA;
}
if (actions.contains(PlayerAction.LEFT)) {
x -= DELTA;
}
if (actions.contains(PlayerAction.RIGHT)) {
x += DELTA;
}
if (y < 0) {
y = 0;
}
if (y + currentBounds.height > size.height) {
y = size.height - currentBounds.height;
}
if (x < 0) {
x = 0;
}
if (x + currentBounds.width > size.width) {
x = size.width - currentBounds.width;
}
getBounds().setLocation(x, y);
}
#Override
public void paint(Graphics2D g2d) {
g2d.setColor(getFillColor());
g2d.fill(getBounds());
}
}
}
Other considerations...
Right now the speed/delta is actually really high. I would consider making use of the Shape API, using things like Point2D and Rectangle2D which provide double based properties instead of int, which would give away to reduce the delta values and slow down the entities.
See Working with Geometry for some more details

Saving bitmaps to ArrayList makes Bitmaps whole black

I am trying to save bitmaps in an ArrayList. I generate 2 types of bitmaps, they are basically identical - drawn with a finger by the user.
The first bitmap is fully visible in activity
The second is stored dynamically in the ArrayList - it is a slice of the last x milliseconds from the visible bitmap - It will be further converting and sending by BT.
The problem is that the last bitmap in the ArrayList is well saved, while each previous bitmap turns into a black rectangle when you add another one.
Relevant code snippets:
private ArrayList<Bitmap> bitmapsListToSend = new ArrayList<>();
private ArrayList<Canvas> canvasListToSend = new ArrayList<>();
mBitmapToShow = Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565);
mCanvasToShow = new Canvas(mBitmapToShow);
bitmapsListToSend.add(Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565))
//mCanvasToSend = new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1));
canvasListToSend.add(new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1)));;
I tried to make separate Canvas to each bitmap without success
public class PaintView extends View {
private final Paint mPaintToShow = new Paint();
private Bitmap mBitmapToShow;
private Canvas mCanvasToShow;
private final Paint mPaintToSend = new Paint();
//private Canvas mCanvasToSend;
private TouchPoint startPoint;
#Nullable
private ArrayList<Bitmap> bitmapsListToSend = new ArrayList<>();
private ArrayList<Canvas> canvasListToSend = new ArrayList<>();
private int bitmapCounter = 0;
private int alpha = 1;
/* debug */
Stopwatch watch = Stopwatch.createUnstarted();
public PaintView(Context c) {
super(c);
setFocusable(true);
mPaintToShow.setAntiAlias(true);
mPaintToShow.setColor(BLACK);
mPaintToShow.setAlpha(alpha);
mPaintToShow.setStrokeJoin(Paint.Join.ROUND);
mPaintToShow.setStrokeCap(Paint.Cap.ROUND);
mPaintToSend.setAntiAlias(true);
mPaintToSend.setColor(BLACK);
mPaintToSend.setStrokeJoin(Paint.Join.ROUND);
mPaintToSend.setStrokeCap(Paint.Cap.ROUND);
mBitmapToShow = Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565);
mCanvasToShow = new Canvas(mBitmapToShow);
bitmapsListToSend.add(Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565));
//mCanvasToSend = new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1));
canvasListToSend.add(new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1)));
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCanvasToShow.drawColor(Color.WHITE);
//mCanvasToSend.drawColor(Color.WHITE);
canvasListToSend.get(canvasListToSend.size()-1).drawColor(WHITE);
}
public void clear() {
if (mCanvasToShow != null) {
mPaintToShow.setColor(Color.WHITE);
mCanvasToShow.drawPaint(mPaintToShow);
//mCanvasToSend.drawPaint(mPaintToShow);
bitmapCounter=0;
alpha=1;
bitmapsListToSend.clear();
canvasListToSend.clear();
mPaintToShow.setColor(BLACK);
watch.reset();
invalidate();
}
}
#Override
protected void onDraw(Canvas canvas) {
if (mBitmapToShow != null) {
canvas.drawBitmap(mBitmapToShow, 0, 0, null);
//mCanvasToSend.drawBitmap(bitmapsListToSend.get(bitmapsListToSend.size()-1), 0, 0, null);
}
}
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event) {
return onTouchOrHoverEvent(event, true /*isTouch*/);
}
private boolean onTouchOrHoverEvent(MotionEvent event, boolean isTouch) {
/*if (SENDING_NOW)
return true;*/
final int action = event.getActionMasked();
mPaintToShow.setStrokeWidth((float) ( getBrushSize() ));
if (action == MotionEvent.ACTION_DOWN) {
startPoint = new TouchPoint(event, 0);
runnableSaveBitmaps.run();
runnableChangeAlpha.run();
watch.start();
}else if(action == MotionEvent.ACTION_MOVE){
runnableChangeAlpha.run();
}else if (action == MotionEvent.ACTION_UP) {
startPoint = new TouchPoint(event, 0);
handlerSaveBitmaps.removeCallbacks(runnableSaveBitmaps);
handlerChangeAlpha.removeCallbacks(runnableChangeAlpha);
watch.stop();
}
//make drawings//
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
paintLines(mTouchPoints);
break;
case MotionEvent.ACTION_DOWN:
mCanvasToShow.drawCircle(startPoint.x, startPoint.y, getBrushSize()/2f, mPaintToShow);
//mCanvasToSend.drawCircle(startPoint.x, startPoint.y, getBrushSize()/2f, mPaintToSend);
canvasListToSend.get(canvasListToSend.size()-1).drawCircle(startPoint.x, startPoint.y, getBrushSize()/2f, mPaintToSend);
break;
}
invalidate();
}
return true;
}
//drawing methods//
Handler handlerSaveBitmaps = new Handler();
Runnable runnableSaveBitmaps = new Runnable() {
public void run() {
handlerSaveBitmaps.postDelayed(runnableSaveBitmaps, 10);
Log.v("B", "Bitmap added " + bitmapCounter + " " + (watch.toString()));
bitmapsListToSend.add(bitmapCounter, Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565));
canvasListToSend.add(bitmapCounter++, new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1)));
}
};
Handler handlerChangeAlpha = new Handler();
Runnable runnableChangeAlpha = new Runnable() {
#Override
public void run() {
handlerChangeAlpha.postDelayed(runnableChangeAlpha, 635);
if(alpha < 255)
mPaintToShow.setAlpha(alpha++);
else {
handlerChangeAlpha.removeCallbacks(runnableChangeAlpha);
Log.v("A", "ALPHA MAX" + (watch.toString()));
}
}
};
}
If Runnable saves 100 bitmaps - 99 will be black, the last one will be ok
//Bitmap.copy() fixed it

Programmatically move ImageView(s) from top to bottom in a loop, starting and ending outside of screen

I'm making a small Android game in which I need two ImageViews (obstacles) to move, at the same time, from the top of the screen to the bottom of the screen; starting and ending OUTSIDE of the screen. Doing this would be a start.
I've got a timer, and each time the timer ends, it runs again and I would also want two other ImageViews (same as two previous ones) to move in the same way described above.
In other words:
Some random event triggers a timer
Timer hits threshold -> timer resets and two ImageViews start their way down
What I've got kind of works, but with Bitmaps and Y incrementation. The problem is, because the phone increments as fast as it can, the game can get laggy and the speed of the two images going down won't be the same on two different phones.
I know I've got to use Animations, but I can't get what I want each time I try something with them. They usually don't start where I want them to be and weird stuff happens.
Here are some links I already visited:
Start simple Animation top to bottom from a specific position
Move an ImageView to different position in Animated way in Android
move imageView from outside screen
Android continuous moving animation
How to slide an ImageView from left to right smoothly in Android?
Here's some code to show you what I've got:
GAMEVIEW
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
// [...]
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
// [...]
this.obstacles = new ArrayList <> ();
this.handleObstacleTimer = null;
this.runnableObstacleTimer = null;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// [...]
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
this.startObstacleTimer();
GameLogic.gameState = GameState.RUNNING;
// [...]
}
return result;
}
#Override
public void draw (Canvas canvas) {
super.draw(canvas);
if (canvas != null) {
this.background.draw(canvas);
if (! this.obstacles.isEmpty()) {
for (Obstacle obstacle : this.obstacles) {
obstacle.draw(canvas);
}
}
// [...]
}
}
public void createObstacle () {
Bitmap imageObstacle = BitmapFactory.decodeResource(this.getResources(), R.drawable.obstacle);
this.obstacles.add(new Obstacle(imageObstacle, this, this.displayMetrics.widthPixels, this.displayMetrics.heightPixels));
}
public void removeObstacle (Obstacle obstacle) {
this.obstacles.remove(obstacle);
}
private void stopObstacleTimer () {
if (this.handleObstacleTimer != null && this.runnableObstacleTimer != null) {
this.handleObstacleTimer.removeCallbacks(this.runnableObstacleTimer);
this.handleObstacleTimer = null;
}
}
private void startObstacleTimer () {
if (this.handleObstacleTimer == null) {
final Handler handler = new Handler();
this.runnableObstacleTimer = new Runnable() {
#Override
public void run() {
if (GameLogic.gameState == GameState.RUNNING) {
createObstacle();
handler.postDelayed(this, 2700);
}
}
};
handler.postDelayed(this.runnableObstacleTimer, 2700);
this.handleObstacleTimer = handler;
}
}
// [...]
// Called by a custom Thread class
public void update () {
if (! this.obstacles.isEmpty()) {
for (Obstacle obstacle : this.obstacles) {
obstacle.update();
}
}
// [...]
}
}
MAINTHREAD (custom Thread class)
public class MainThread extends Thread {
private SurfaceHolder surfaceHolder;
private GameView gameView;
private GameLogic gameLogic;
private boolean running;
public static Canvas canvas;
public MainThread (SurfaceHolder surfaceHolder, GameView gameView, GameLogic gameLogic) {
super();
this.surfaceHolder = surfaceHolder;
this.gameView = gameView;
this.gameLogic = gameLogic;
}
#Override
public void run() {
while (this.running) {
this.canvas = null;
try {
this.canvas = this.surfaceHolder.lockCanvas();
synchronized (this.surfaceHolder) {
this.gameView.update();
this.gameView.draw(this.canvas);
this.gameLogic.update();
}
} catch (Exception e) {
} finally {
if (this.canvas != null) {
try {
this.surfaceHolder.unlockCanvasAndPost(this.canvas);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public void setRunning (boolean isRunning) {
this.running = isRunning;
}
}
OBSTACLE
public class Obstacle {
private static final int GAP_MIN_X = 300;
private static int GAP_MAX_X;
private static final int GAP_WIDTH = 250;
private GameView gameView;
private int displayHeight;
private Bitmap obstacleLeft;
private Bitmap obstacleRight;
private int leftX;
private int rightX;
private int y;
private boolean passed;
public Obstacle (Bitmap image, GameView gameView, int displayWidth, int displayHeight) {
this.gameView = gameView;
this.displayHeight = displayHeight;
this.obstacleLeft = this.flip(image);
this.obstacleRight = image;
GAP_MAX_X = displayWidth - GAP_MIN_X;
final int randomX = new Random().nextInt((GAP_MAX_X - GAP_MIN_X) + 1) + GAP_MIN_X;
this.leftX = randomX - this.obstacleLeft.getWidth() - (GAP_WIDTH / 2);
this.rightX = randomX + (GAP_WIDTH / 2);
this.y = 0 - this.obstacleLeft.getHeight();
this.passed = false;
}
private Bitmap flip (Bitmap image) {
Matrix m = new Matrix();
m.preScale(-1, 1);
Bitmap result = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), m, false);
result.setDensity(DisplayMetrics.DENSITY_DEFAULT);
return result;
}
/**
* Those getters are used for collision
*/
public int getLeftX () {
return this.leftX;
}
public int getRightX () {
return this.rightX;
}
public int getY () {
return this.y;
}
public int getWidth () {
return this.obstacleLeft.getWidth();
}
public int getHeight () {
return this.obstacleLeft.getHeight();
}
public boolean hasPassed () {
return this.passed;
}
public void setAsPassed () {
this.passed = true;
}
public void draw (Canvas canvas) {
canvas.drawBitmap(this.obstacleLeft, this.leftX, this.y, null);
canvas.drawBitmap(this.obstacleRight, this.rightX, this.y, null);
}
public void update () {
if (! (GameLogic.gameState == GameState.GAMEOVER)) {
this.y += 8;
if (this.y > this.displayHeight) {
this.gameView.removeObstacle(this);
}
}
}
}
Basically, what I want is to have the same result. But without lag and with obstacles going at the same speed no matter which phone the game is running on.
If inside your Obstacle.update() function, you change your Y incrementation from the constant integer 8 to something that is proportional to the time interval that the update function is called, it will be the same speed on any device. You could, for example, calculate the variation based on the difference between the time on the last update() call and the current update call
long speed = 100; //you should add this to adjust the speed of the incrementing
long delta = System.currentTimeMillis();
public void update () {
delta = speed*(System.currentTimeMillis() - delta);
if (! (GameLogic.gameState == GameState.GAMEOVER)) {
this.y += delta;
if (this.y > this.displayHeight) {
this.gameView.removeObstacle(this);
}
}
}
This way, you delta variable will ever be something that varies with the time interval between the last call and the current call beacause System.currentTimeMillis() returns the current time in miliseconds

LibGDX Button does not recognize clicks (Stage + ChangeListener)

EDIT: First Cemmentator "Second" was right. I forgot to set Gdx.input.setInputProcessor(stage); - Now everything is working as it should. Thanks!
My Libgdx Buttons "Back", "Menu" and "TryAgain" (which bring you back to another Screen) are not working.
I worked through every solution that I could find but they still not work properly. I also use Buttons and stages in my other Screens and they all work like a charm.
Do you guys see any mistakes in the code? CreateStage() creates the stage and they get displayed through the render Method.
Any Help is greatly appreciated!
public class HighscoreScreen extends ScreenAdapter {
private final GameClass game;
private int score;
private String name;
private boolean menuView;
private SpriteBatch batch;
private BitmapFont scoreFont;
private String[] players;
private int[] scores;
private int counter = 0;
private OrthographicCamera camera;
private Stage stage;
private ScreenViewport v;
public HighscoreScreen (GameClass game, OrthographicCamera camera, int score, String name) {
this.camera = camera;
this.game = game;
this.score = score;
this.name = name;
this.batch = new SpriteBatch();
menuView = false;
batch = new SpriteBatch();
scoreFont = new BitmapFont(Gdx.files.internal("Fonts/score.fnt"),
Gdx.files.internal("Fonts/score.png"),false);
createStage();
}
public HighscoreScreen (GameClass game, OrthographicCamera camera){
this.game = game;
this.camera = camera;
this.batch = new SpriteBatch();
menuView = true;
scoreFont = new BitmapFont(Gdx.files.internal("Fonts/score.fnt"),
Gdx.files.internal("Fonts/score.png"),false);
}
public void createStage(){
v = new ScreenViewport();
v.setCamera(camera);
stage = new Stage(v, batch);
if (menuView) {
Buttons back = new Buttons("Back");
stage.addActor(back.createButton(GameInfo.WIDTH / 2 - 100,
GameInfo.HEIGHT / 2 - 200));
back.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MenuScreen(game));
}
});
} else {
Buttons tryAgain = new Buttons("Try Again");
stage.addActor(tryAgain.createButton(GameInfo.WIDTH / 2 - 200,
GameInfo.HEIGHT / 2 - 200));
tryAgain.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new SurvivalScreen(game, name));
}
});
Buttons menu = new Buttons("Menu");
stage.addActor(menu.createButton(GameInfo.WIDTH / 2,
GameInfo.HEIGHT / 2 - 200));
menu.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MenuScreen(game));
}
});
}
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (menuView) {
if (counter == 0) {
createStage();
writeScoresToFile();
readHighscore();
counter++;
}
batch.begin();
highscoreToFont();
batch.end();
stage.act();
stage.draw();
} else {
if (counter == 0) {
createStage();
writeScoresToFile();
readHighscore();
compareHighscore();
writeHighscore();
counter++;
}
batch.begin();
highscoreToFont();
batch.end();
stage.act();
stage.draw();
}
}
public void dispose(){
batch.dispose();
stage.dispose();
}
You are probably missing the settings of the input processor to your stage (at least it's not in the code you have posted).
Gdx.input.setInputProcessor(stage);
Note:
Assuming the the Buttons class returns a proper actor.
(see my comment above)

Android: Low frame rate

I have created an application that holds 7 different mini games. One of these mini games has always run fine, well, it always had some frame issues usually running at 6fps. I ran this game recently and realized the frame rate is now pretty much unplayable. I don't what caused this issue and can anyone recommend a fix to improve the frame rate?
The game uses many different classes to be created, it is a surface view. Copying all the classes here will be a lot of code so I will just post the class that creates the surface.
Any help with improving an application frame rate would be helpful thank you.
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{
public static final int WIDTH = 856;
public static final int HEIGHT = 480;
public static final int MOVESPEED = -5;
private long missileStartTime;
private MainThread thread;
private Background bg;
private Player player;
private ArrayList<Missile> missiles;
private Random rand = new Random();
int score = 0;
private final Context context;
public GamePanel(Context context)
{
super(context);
this.context = context;
//add the callback to the surfaceholder to intercept events
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
//make gamePanel focusable so it can handle events
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}
#Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
int counter = 0;
while(retry && counter<1000)
{
counter++;
try{thread.setRunning(false);
thread.join();
retry = false;
}catch(InterruptedException e){e.printStackTrace();}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder){
bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.nightskyresize));
player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.pilot), 65, 25, 1);
missiles = new ArrayList<Missile>();
missileStartTime = System.nanoTime();
//we can safely start the game loop
thread.setRunning(true);
thread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(!player.getPlaying())
{
player.setPlaying(true);
}
else
{
player.setUp(true);
}
return true;
}
if(event.getAction()==MotionEvent.ACTION_UP)
{
player.setUp(false);
return true;
}
return super.onTouchEvent(event);
}
public void update()
{
if(player.getPlaying()) {
bg.update();
player.update();
//add missiles on timer
long missileElapsed = (System.nanoTime()-missileStartTime)/10000000;
if(missileElapsed >(2000 - player.getScore()/4)){
System.out.println("making missile");
//first missile always goes down the middle
if(missiles.size()==0)
{
missiles.add(new Missile(BitmapFactory.decodeResource(getResources(),R.drawable.
missile),WIDTH + 10, HEIGHT/2, 45, 15, player.getScore(), 1));
}
else
{
missiles.add(new Missile(BitmapFactory.decodeResource(getResources(),R.drawable.missile),
WIDTH+10, (int)(rand.nextDouble()*(HEIGHT)),45,15, player.getScore(),1));
}
//reset timer
missileStartTime = System.nanoTime();
}
//loop through every missile and check collision and remove
for(int i = 0; i<missiles.size();i++)
{
//update missile
missiles.get(i).update();
if(collision(missiles.get(i),player))
{
Intent i1 = new Intent (context, main_menu.class);
context.startActivity(i1);
missiles.remove(i);
player.setPlaying(false);
break;
}
//remove missile if it is way off the screen
if(missiles.get(i).getX()<-100)
{
score++;
missiles.remove(i);
break;
}
}
}
if(score == 100){
Intent i1 = new Intent (context, main_menu.class);
context.startActivity(i1);
}
}
public boolean collision(GameObject a, GameObject b)
{
if(Rect.intersects(a.getRectangle(),b.getRectangle()))
{
return true;
}
return false;
}
#Override
public void draw(Canvas canvas)
{
final float scaleFactorX = getWidth()/(WIDTH*1.f);
final float scaleFactorY = getHeight()/(HEIGHT*1.f);
if(canvas!=null) {
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);
bg.draw(canvas);
player.draw(canvas);
//draw missiles
for(Missile m: missiles)
{
m.draw(canvas);
}
drawText(canvas);
canvas.restoreToCount(savedState);
}
}
public void drawText(Canvas canvas)
{
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(30);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
canvas.drawText("DISTANCE: " + (player.getScore()*3), 10, HEIGHT - 10, paint);
}
}
I also got the log to print out the frame rate, this was the result.
Any ideas on the cause of this? the fps is the 0.0, this usually would say around 6.0 or 7.0.
This is an odd question, but where I have music it sounds like there is two versions playing over each other, like the application is running two instances of itself. is this possible?
This is the problem

Categories