Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing velocity to move rectangle in javascript using processing.js
    primarykey
    data
    text
    <p>I am working on a simple pong clone using javascript and processing.js . I have made classes for the paddles, which are then extended to make a class which will be controlled by the player(s). Currently, I am attempting to implement handling for keyboard input within the player controlled class. My intent is that when <code>w</code> or <code>s</code> is pressed, I update the position of the player's paddle through a velocity represented by a <code>pVector</code> variable within the player class. </p> <p>However, when the corresponding keys are pressed, currently the paddle simply disappears.</p> <p>The script can be seen on jsfiddle <a href="http://jsfiddle.net/ma6rt/9/" rel="nofollow">here</a>, and My code is as follows:</p> <pre><code> // For reference // standard rectangle constructor is: rect(x, y, width, height); class Paddle { //width of paddle float pWidth; //height of paddle float pHeight; //initial paddle x coordinate float x; //initial paddle y coordinate float y; Paddle(float w, float h, float startX, float startY) { //set width paddleWidth = w; //set height paddleHeight = h; //set start x x = startX; //set start y y = startY; } void update() { } void draw() { //draw and fill rectangle with white fill(255) rect(x,y,paddleWidth,paddleHeight) } } class Player extends Paddle { Player(float w, float h, float startX, float startY) { super(w,h,startX,startY); } } class PlayerOne extends Paddle { pVector playerVelocity = (0,0); PlayerOne(float w, float h, float startX, float startY) { super(w,h,startX,startY); } void update() { debugger; if(keyPressed) { if(key == 'w') { y -= playerVelocity.y; } else if(key == 's') { y += playerVelocity.y; } } } } //array list to hold the player paddles ArrayList&lt;Paddle&gt; paddles = new ArrayList&lt;Paddle&gt;(); //middle x and middle y float mx, my, pw, ph; void setup() { mx = width/2; my = height/2; pw = 10; ph = 50; player1 = new PlayerOne(pw,ph,10,10); player2 = new Player(pw,ph,385,10); paddles.add(player1); paddles.add(player2); size(400,400); } void draw() { background(0); fill(100,100,0); // update each paddle added to array list for(Paddle p: paddles) { p.update(); p.draw(); } } </code></pre> <p>What am I doing wrong?</p> <p>UPDATE:</p> <p>I put a breakpoint in with the line <code>debugger</code> after the condition on key press: <code>if(keyPressed)</code> . It seems that if the key is pressed once, it is detected repeatedly on every update for some reason. </p>
    singulars
    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.
 

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