Note that there are some explanatory texts on larger screens.

plurals
  1. POMy pong paddle is not operating right
    primarykey
    data
    text
    <p>I'm still fairly new to android and I'm currently trying to create pong. </p> <p>At the moment I am trying to animate two images, one is pong's ball and the second is the paddle. Right now I have those two images and I've got them animating. The ball behaves like I want it to bouncing around the x and y axis and I have the paddle sliding back and forth across the x axis but for some reason the paddle behaves weirdly. </p> <p>The paddle for some reason animates choppy, stopping and starting whenever it wants. But whenever I remove the ball animation from the code the paddle acts and behaves like I intended it to</p> <p>If anyone has any suggestion for what I am doing wrong or overlooking, I am all ears, Thanks!</p> <pre><code>package com.example.pongtest; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.widget.ImageView; import android.widget.LinearLayout; import android.app.Activity; public class MainActivity extends Activity { LinearLayout lay1; ImageView ballImg, paddleImg; TimerTask thisTimerTask; Timer thisTimer; LinearLayout.LayoutParams params; float ballPosX=10; float ballPosY=10; float paddlePosX=200; float paddlePosY=10; float ballOnLeftWall=0; float ballOnRightWall=270; float ballOnBottomWall=400; float ballOnTopWall=0; float paddleOnLeftWall=0; float paddleOnRightWall=200; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lay1 = new LinearLayout(this); ballImg = new ImageView(this); ballImg.setBackgroundResource(R.drawable.circlewdot); setPosition(0,0,50,50); ballImg.setLayoutParams(params); paddleImg=new ImageView(this); paddleImg.setBackgroundResource(R.drawable.paddle); setPosition(0,0,50,10); paddleImg.setLayoutParams(params); lay1.addView(ballImg); lay1.addView(paddleImg); setContentView(lay1); thisTimerTask = new ThisTimerClass(); thisTimer = new Timer(); //setup frame rate of timer thisTimer.scheduleAtFixedRate(thisTimerTask, 2000, 16); } public void setPosition(float x, float y, float width, float height) { params = new LinearLayout.LayoutParams(0,0); params.topMargin = (int)y; params.leftMargin = (int)x; params.width = (int)width; params.height = (int)height; } class ThisTimerClass extends TimerTask { boolean directionOfBall_isRight = true; boolean directionOfBall_isDown = true; boolean directionOfPaddle_isLeft = true; @Override public void run() { // Will be called each time the timer is fired. MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { setPosition(ballPosX,ballPosY,50,50); ballImg.setLayoutParams(params); setPosition(paddlePosX,paddlePosY,50,10); paddleImg.setLayoutParams(params); //animates ball across the screen if (directionOfBall_isRight == true) {ballPosX = (ballPosX + 1);} else if (directionOfBall_isRight == false) {ballPosX = (ballPosX - 1);} if (directionOfBall_isDown == true) {ballPosY = (ballPosY + 1);} else if (directionOfBall_isDown == false) {ballPosY = (ballPosY - 1);} //redirects ball if (ballPosX &gt; ballOnRightWall) {directionOfBall_isRight = false;} else if (ballPosX &lt; ballOnLeftWall) {directionOfBall_isRight = true;} if (ballPosY &gt; ballOnBottomWall) {directionOfBall_isDown = false;} else if (ballPosY &lt; ballOnTopWall) {directionOfBall_isDown = true;} //animates paddle across the screen if (directionOfPaddle_isLeft == true) {paddlePosX = (paddlePosX - 1);} else if (directionOfPaddle_isLeft == false) {paddlePosX = (paddlePosX + 1);} //redirects paddle if (paddlePosX &lt; paddleOnLeftWall) {directionOfPaddle_isLeft = false;} else if (paddlePosX &gt; paddleOnRightWall) {directionOfPaddle_isLeft = true;} } }); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload