A bit choppy/laggy object movement.
in
Programming Questions
•
1 year ago
So I'm trying to make a game in Processing, and i've got this source here:
- PlayerRect mov = new PlayerRect(new PVector(40, 94), 5, 5, 0, 0, 255);
final int windowX = 800;
final int windowY = 600;
void setup() { size(windowX,windowY); }
void draw() {
background(150);
mov.update();
mov.render();
mov.moveself();
} - class Rectangle {
PVector pos;
int h;
int w;
int r;
int g;
int b;
Rectangle (PVector inpos, int inw, int inh, int inr, int ing, int inb) { //define with a vector
pos = inpos;
h = inh;
w = inw;
r = inr;
g = ing;
b = inb;
}
void render () {
fill(r, g, b);
noStroke();
rect(pos.x, pos.y, w, h);
}
float getCX() { //get center-x
return pos.x-(w/2);
}
float getCY() { //get center-y
return pos.y-(h/2);
}
}
class MotileRect extends Rectangle {
PVector speed; //in pixels per 1/10th second.
Trigger timer;
MotileRect(PVector inpos, int inw, int inh, int inr, int ing, int inb, PVector inspd) {
super(inpos, inw, inh, inr, ing, inb);
speed = inspd;
timer = new Trigger(10);
}
void update() {
while (timer.fires ()) {
pos.add(speed);
}
}
}
class PlayerRect extends MotileRect {
PlayerRect(PVector inpos, int inw, int inh, int inr, int ing, int inb) {
super(inpos, inw, inh, inr, ing, inb, new PVector(0,0)); //start out speedless
}
void moveself() {
if (checkKey("W")) {
speed.y -= .1;
}
if (checkKey("S")) {
speed.y += .1;
}
if (checkKey("A")) {
speed.x -= .1;
}
if (checkKey("D")) {
speed.x += .1;
}
}
}
boolean collDetect(Rectangle rect1, Rectangle rect2) {
if (rect1.pos.x+rect1.w < rect2.pos.x) {
return false;
}
if (rect1.pos.x > rect2.pos.x+rect2.w) {
return false;
}
if (rect1.pos.y+rect1.h < rect2.pos.y) {
return false;
}
if (rect1.pos.y > rect2.pos.y+rect2.h) {
return false;
}
return true;
} - class Trigger { //by kritzikratzi
long start = millis();
int rate;
public Trigger( int rate ) {
this( rate, false );
}
public Trigger( int rate, boolean immediately) {
setRate( rate );
if ( immediately ) start -= rate;
}
public void setRate( int rate ) {
this.rate = rate;
}
public int getRate() {
return rate;
}
public boolean fires() {
if ( millis() - start >= rate ) {
start += rate;
return true;
}
else {
return false;
}
}
public void reset() {
start = millis();
}
} - boolean[] keys = new boolean[526];
boolean checkKey(String k) {
for (int i = 0; i < keys.length; i++) {
if (KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) {
return keys[i];
}
}
return false;
}
void keyPressed()
{
keys[keyCode] = true;
//println(KeyEvent.getKeyText(keyCode));
}
void keyReleased()
{
keys[keyCode] = false;
}
It's a bit choppy when i move that rect, even though it have a high-end graphics card. Why is this?
1