hello, i’m working on a little game for a uni project and i got stuck trying to move an object using keyboards. depending where i move the void keypressed part the key might get spammed or not show at all, but the object still wont move.
this is the main file
Timer t;
Enemie e;
Player p;
void setup (){
//window setup
size(800, 800);
background(0);
smooth();
t= new Timer();
e= new Enemie();
p= new Player();
}
void draw (){
background(0);
t.display();
e.run();
p.run();
}
void keyPressed() {
println(key);
if (key == 'a') {
p.x--;
} else {
keyReleased();
}
}
this is the file where im creating the player (movable object)
thanks for replying and this deffinately works as i want to make my shape to work but i dont really understand what is going on. I’m completely new to this and its for a final piece project so i have to be able to explain myself. I hoped there is a easier way to make it move and i was just doing something wrong myself.
ok so what you told me works great thanks @Chrisir . I added the whole thing i hope its fine now, i only added the main file and the players one at first because the other 2 dont really have anything to do with what was going on.
I made the p.x+ and - on the movement 15 to make it move faster but the way it looks when it moves its a bit clunky. And if i use p.x++ and – it moves way to slow but it looks smooth.
My last question before i move on to making it shoot and destroy things, is there a way to make it move fast but smooth at the same time?
edit: And maybe allow W+A and W+D / S+A / S+D work at the same time. i added under the a and d the same thing for w and s but they only work separately.
//GameMode File
//initializing classes
Timer t;
Enemy e;
Player p;
void setup (){
//window setup
size(800, 800);
background(0);
smooth();
t= new Timer();
e= new Enemy();
p= new Player();
}
void draw (){
background(0);
t.display();
e.run();
p.run();
}
void keyPressed() {
println(key);
if (key == 'a') {
p.x -= 15;
} else if (key == 'd') {
p.x += 15;
}
if (key == 'w') {
p.y -= 15;
} else if (key == 's') {
p.y += 15;
}
}
//Enemy File
class Enemy {
//class variables
float[] x = new float[15];
float[] y = new float[15];
float[] m = new float[15]; //speed of falling
float a; //shake speed
float noiseScale; //shake intensity
float sX; //shake on X axis
//class constructor
Enemy() {
for (int i=0; i<15; i++){
x[i] = random(width);
y[i] = (TOP-200);
m[i] = random(1,5);
a=0;
noiseScale=110;
}
}
//class functions
void run() {
display();
life();
shoot();
}
void display() {
//circles color
stroke(255);
fill(#F70000);
//drawing the shake
float sX = noise(a)*noiseScale;
a+= 0.1;
//drawing the circles and movement
for (int i=0; i<15; i++){
ellipse(x[i]+ sX ,y[i], 50, 50);
y[i] = y[i] + m[i];
// if the circle goes past the bottom it will reapear on top
if (y[i] > height) {
y[i] = TOP-200;
}
}
}
void life() {
}
void shoot() {
}
}
//Player File
class Player {
//class variables
float x;
float y;
PShape t;
//class constructor
Player() {
x= 0;
y= 0;
t = createShape();
t.beginShape();
t.fill(#FFF303);
t.stroke(255);
t.vertex(x, y); //nose
t.vertex(x-10, y+20);
t.vertex(x-20, y-20); //left peak
t.vertex(x-90, y+90); //left wing
t.vertex(x-30, y+60); // left ass
t.vertex(x+30, y+60); // right ass
t.vertex(x+90, y+90); //right wing
t.vertex(x+20, y-20); //right peak
t.vertex(x+10, y+20);
t.endShape(CLOSE);
x= width/2;
y= height*0.8;
}
//class functions
void run() { //function container
display();
shoot();
life();
}
void display() {
pushMatrix();
translate(x, y);
fill(#03FFDB);
stroke(#030CFF);
strokeWeight(2);
shape(t);
popMatrix();
}
void shoot() {
}
void life() {
}
}
//Timer File
class Timer {
//class variables
int mstime = millis(); //giving the miliseconds a value
int minutes;
int seconds;
float x; //x of the whole box
float y; //y of the whole box
//class constructor (defining)
Timer() {
x= width*0.003;
y= height*0.003;
}
//class functions
void display() {
fill(0);
stroke(255);
strokeWeight(1);
rect(x,y, 93, 37);
if ( millis() > mstime + 1000)
{
mstime= millis();
seconds = seconds +1;
}
if (seconds > 59)
{
minutes++;
seconds = 0;
}
fill(255);
textAlign(RIGHT);
textSize(30);
if( minutes <=9) {
text("0", x+24, y+30);
}
text(minutes, x+44, y+30);
if( seconds <=9) {
text("0", x+69, y+30);
}
text(seconds, x+89, y+30);
text( ":", x+51, y+28);
}
}
yes, making a boolean array and having the keys true when pressed and false when released allows 2 keys to be pressed at once. Thx for all the help, now i can move on to making things shoot and die
this is how ive done it:
void keyPressed() {
//move on x axis
if (key == 'a') {
p.keys[0]=true;
}
if (key == 'd') {
p.keys[1]=true;
}
//move on y axis
if (key == 'w') {
p.keys[2]=true;
}
if (key == 's') {
p.keys[3]=true;
}
}
void keyReleased() {
//move on x axis
if (key == 'a') {
p.keys[0]=false;
}
if (key == 'd') {
p.keys[1]=false;
}
//move on y axis
if (key == 'w') {
p.keys[2]=false;
}
if (key == 's') {
p.keys[3]=false;
}
}
//setting the distance value of wasd movement
void movement() {
if(p.keys[0]){
p.x -= 15;
}
if(p.keys[1]){
p.x += 15;
}
if(p.keys[2]){
p.y -= 15;
}
if(p.keys[3]){
p.y += 15;
}
}
i wanted to have this whole thing in the player class file but it only works in the main file. i decided to define the array in there tho to keep the main file more clean.
edit: moving only the void movement to the player file works. but keypressed and keyreleased must stay in the main file.
You can have keypressed and keyreleased in the main file
AND have a keypressedClass and keyreleasedClass in the class and call it from
keypressed and keyreleased in the main file
i went ahead adding a bullet class so i can start shooting, but i cant manage to make it fallow the player around.
This is the bullets class, it either goes up all the time but not fallwing the X of the ship, or if i add x= p.x; withing the display void the bullet will go left and right depending on how i move the ship
i just want to have to bullets flying without having to press anything. i will eventuallly add a powerup to shoot with 2 bullets at once or 3 bullets at once.
and i will have to add colission for them to be able to kill some “asteroids” , so ill need them to keep fire even if the bullet gets destroyed on colission. so the way i have it now to return when it reaches the top wont work in the end.