Android - Retro Squash Game - racket/screen sides collision - java

I am reading "Learning Java by Building Android Games" and in a Retro Squash Game example I don't know how to implement collision detection for the racket. The movement of the racket uses onTouchEvent. I have tried to implement an if statement but it deosn't get checked until the next touch event- so it doesn't work properly. Please help.
//Event that handles in which direction is the racket moving according to where is the user touching the screen
#Override
public boolean onTouchEvent(MotionEvent motionEvent) {
//gets the movement action without the pointers(ACTION_MASK) ??? -U: handles multitouch
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
//what happens if user touches the screen and holds
case MotionEvent.ACTION_DOWN:
//if the screen was touched on the right side of the display than move the racket right
if (motionEvent.getX() >= (screenWidth / 2) && racketPosition.x <= screenWidth) {
racketIsMovingLeft = false;
racketIsMovingRight = true;
} else if (motionEvent.getX() < (screenWidth / 2) && racketPosition.x >= 0) {
racketIsMovingLeft = true;
racketIsMovingRight = false;
} else {
racketIsMovingLeft = false;
racketIsMovingRight = false;
}
break;
//when the user lets go of the screen the racket immediately stops moving
case MotionEvent.ACTION_UP:
racketIsMovingLeft = false;
racketIsMovingRight = false;
break;
}
return true;
}

Ok. I have found the solution. I wasn't looking at the right piece of code - sorry. I have edited the piece which is responsible for changing the racketPoint.x of the racket. Here it is:
public void updateCount() {
//change the racket position according to the movement
if (racketIsMovingRight && (racketPosition.x+racketWidth/2)<=screenWidth) {
racketPosition.x += racketSpeed;
}
if (racketIsMovingLeft && (racketPosition.x-racketWidth/2)>=0) {
racketPosition.x -= racketSpeed;
}
//rest of the code

Related

How do I make an image(drawable) follow my finger?

I have this class which extends SurfaceView. The code that I have so far makes the player follow my finger, but it also allows the player to "teleport" to wherever the finger goes and I do not want that.
#Override
public boolean onTouchEvent(MotionEvent event) {
pointerX = (int)event.getX();
pointerY = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!player.getPlaying()) {
player.setPlaying(true);
}
if (!player.playerAlive) {
if ((pointerX >= rescaleX(600) && pointerX <= rescaleX(842)) && (pointerY >= rescaleY(900) && pointerY <= rescaleY(1142))) {
//newGame();
player.setX(600);
player.setY(900);
}
}
if (player.playerAlive) {
// player.move(true);
}
break;
case MotionEvent.ACTION_MOVE:
if (!player.getPlaying()){
player.setPlaying(true);
player.move(true);
} else {
player.move(true);
}
break;
case MotionEvent.ACTION_UP:
//player.move(true);
if (!player.playerAlive) {
if ((pointerX >= rescaleX(600) && pointerX <= rescaleX(842)) && (pointerY >= rescaleY(900) && pointerY <= rescaleY(1142))) {
newGame();
player.setX(600);
player.setY(900);
}
}
break;
}
return true;
//return super.onTouchEvent(event);
}
Here is the move() method in the player class:
public void move(boolean b) {
if(b){
if((GamePanel.pointerX * GamePanel.WIDTH / MainActivity.dispX) - 141 >= 1380 - getWidth()){setX(1380-getWidth());}
if((GamePanel.pointerY * GamePanel.HEIGHT / MainActivity.dispY) - 141 >= 1880 - getHeight()){setY(1880 - getHeight());}
setX((int) ((GamePanel.pointerX * GamePanel.WIDTH / MainActivity.dispX) - 141));
setY((int) ((GamePanel.pointerY * GamePanel.HEIGHT / MainActivity.dispY) - 141));
}
Don't "directly" translate finger inputs into 'logic' for moving things.
You should have your onTouchEvent method reads the X and Y values of where a finger press is, and then passes that to some logic. That logic should then decide if that info should be passed to a new method moveCharacterTo(x,y)
the 'logic' I mention above could be several things, You could use the strategy pattern to decide what 'strategy/policy(algorithm)' should be used to process the screen. Or you could just have a single class with a method to compare touch events and see if they are 'close' to the character or not, and then react accordingly.

JFrame skipping some repaint() calls

I'm making a snake game and I've encountered an error.
I have tried two different loops: thread.sleep and Timer.schedule.
I have gotten the same problem.
It will be working fine, but at random intervals, it will start to skip every other frame for 6-10 frames.
In case I wasn't clear, 1 frame is
#Override public void paintComponent(Graphics G){...}
being called. (I have also tried paint)
This has occurred in some other games I've created, but not all. What can I do to fix it?
Here's a full copy of the code:
https://github.com/jnmcd/Snake/blob/master/Code.java
EDIT: I've done some debugging. It appears that the it's not a problem with the paint. The JPanel doesn't always update. What can I do to fix it?
I found what I needed to do. I had to add a revaidate() after the repaint().
Also In the checkKillCollisions you have to break the loop right after you found the losing condition.
Also if the game ends it keeps on showing the error message[Dialog] for which there i no end.So I have created a flag gameOver to check is game is over or not in Snake Class
static Boolean gameOver = false;//Defined in Snake Class
public void checkKillCollisions() {
boolean lose = false;
for (int i = 1; i < Snake.segments.size(); i++) {
if (Snake.segments.get(i).x == x && Snake.segments.get(i).y == y) {
lose = true;
break;//Have to do this
}
}
if (x >= 60 || x < 0 || y >= 60 || y < 0) {
lose = true;
}
if (lose) {
Snake.window.popUp("You Lose");
}
Snake.gameOver = lose;//Will set the gameOVer flag in Snake class
}
And I've modified the Loop class to stop running right after the gameOver flag is set to true
class Loop extends TimerTask {
#Override
public void run() {
if (!Snake.gameOver) {
Snake.updates();
Snake.window.render();
} else {
System.out.println("Game Over");
cancel();
Snake.window.dispose();
}
}
}

game state manager cannot be resolved in my gamestate package

Im a 2d java platform developer. I need help with the gamestate manager. Im making a death sequence and would simply like to reroute the game back to the menu upon dying. My code is currently this (do keep In mind I have a gamestate manager aswell as a game state of course, and I use java eclipse.):
public void hit(int damage) {
if(flinching) return;
health -= damage;
if(health < 0) health = 0;
if(health == 0) dead = true;
if (dead == true) gsm.setState(GameStateManager.MENUSTATE);
flinching = true;
flinchTimer = System.nanoTime();
Please help

Collision detection with bitmap Android

Alright, so i cant figure out how to make this rpg game im working for android have collision.
I draw the map on the screen in a drawable portion that the character is located in. I move the character by a 64 amount across the screen and when it hits the bounds of the screen it will then move the map and look like the character is moving across the map. It stop when the map hits the end it doesnt move the screen any more. What im trying to do is figure out a away to check if my character is in a certain bitmap and not let it pass through. Here is the code for my player moving, and the map itself. The character and the tiles are bitmaps.
Anything else needed to allow you to help me better comment and i will post more and how it works.
Edit:
sp.yp and sp.xp is the position of the character on screen.
This Draw the map to the screen:
public void draw(Canvas canvas){
//How many tiles are on the screen length times width
for(int x = 0; x <= 31;x++){
for(int y = 0; y <= 17;y++){
switch(Map[Mapx + x][Mapy + y]){
case 0:
canvas.drawBitmap(BLOCK_ROCK, x*32,y*32,null);
break;
case 1:
canvas.drawBitmap(BLOCK_OCEAN, x*32,y*32,null);
break;
case 2:
canvas.drawBitmap(BLOCK_GRASS, x*32,y*32,null);
break;
case 3:
canvas.drawBitmap(BLOCK_ROCK, x*32,y*32,null);
break;
case 4:
canvas.drawBitmap(BLOCK_FLOWER, x*32,y*32,null);
break;
}
}
}
}
This is the movment of the player, these methods are called when person hits the keypad i have drawn to the screen:
public void Down(){
if(sp.yp == 512){
if(w.Mapy == w.mapheight - 17 - 1){
}else{
w.Mapy +=1;
}
}else{
sp.setYd(64);
sp.update();
sp.setYd(0);
}
}
public void Left(){
if(sp.xp == box.xMin + 32){
sp.isRight = false;
if(w.Mapx == 0){
}else{
w.Mapx -=1;
}
}else{
sp.isRight = false;
sp.setXd(-64);
sp.update();
sp.xd = 0;
}
}
public void Jump(){
if(sp.yp == 64){
if(w.Mapy == 0){
}else{
w.Mapy -=1;
}
}else{
sp.setYd(-64);
sp.update();
sp.setYd(0);
}
}
public void Right(){
if(sp.xp == 992){
sp.isRight = true;
if(w.Mapx == w.mapwidth - 31 - 1){
}else{
w.Mapx +=1;
}
}else{
sp.isRight = true;
sp.setXd(64);
sp.update();
sp.xd = 0;
}
}
You should look into a game engine to handle this type of thing. There are several that are easy to import into your project and provide a great deal of functionality so you can work on designing the game and media resources more. Trust me, you probably don't want to code the whole engine.
Check
AndEngine - http://www.andengine.org/
Libgdx - http://code.google.com/p/libgdx/
I suggest AndEngine because there is a great app of simple examples you can use to experiment with. You can find it on:
code.google.com/p/ andengineexamples/
(no space in the address... sorry couldn't post more than two links)

How to react to user tapping more than once

I am using this method in AndEngine to detect when a user taps on the screen,
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) {
if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or something
taps++;
if(taps == 1){
if(isJumping == false){
final float jumpDuration = 2;
final float startX = player.getY();
final float jumpHeight = 100;
final MoveYModifier moveUpModifier = new MoveYModifier(.1f, startX, startX - jumpHeight);
final MoveYModifier moveDownModifier = new MoveYModifier(.1f, startX - jumpHeight, startX);
final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);
player.registerEntityModifier(modifier);
isJumping = true;
hipp_jump.play();
return true;
}
}
}
}
return false;
}
Sooo The issue I am having with this is that if the user double-taps the screen, then the sprite jumps twice which moves him out of the position he should return to. Because when it jumps twice the Y changes.
How can I allow the sprite to move only ONCE to each tap, even if the user taps more than once?
The manual solution is to set a delay (record a timestamp of tap), and ignore taps within some timedelta. I suggest using the high-resolution java.lang.System.nanoTime()
Especially capacitive touchscreens are prone to generate multiple taps even unintentionally. It is not handled in Android, and has proven to be a serious problem for our app...
Update: pseudocode sample
private long lastTap=0;
onTap() {
long now = System.nanoTime();
if (now-lastTap < threshold) return;
else lastTap = now;
...
}

Categories