We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have been trying to make a walking animation for a 2d game i am making and, the image is supposed to alternate images for the steps in the correct direction that you are holding on the keyboard(wasd). When it is supposed to load the character image it flickers the map1 and the character image and then the screen just goes black and returns no errors. Why is this?
code:
import processing.opengl.*;
int time = millis();
int oldtime = 1;
float state = 1;
boolean wPressed=false;
boolean sPressed=false;
boolean aPressed=false;
boolean dPressed=false;
int dir = 1;
int step = 1;
int locx = 500;
int locy = 300;
PImage char1;
PImage char12;
PImage char13;
PImage char2;
PImage char22;
PImage char23;
PImage char3;
PImage char32;
PImage char33;
PImage char4;
PImage char42;
PImage char43;
PImage start;
PImage buttonnew;
PImage buttoncontinue;
PImage map1;
//Code
void setup() {
size(1024, 768);
frameRate(120);
frame.setResizable(true);
imageMode(CORNER);
noStroke();
smooth();
char1 = loadImage("char1.png");
char12 = loadImage("char1-2.png");
char13 = loadImage("char1-3.png");
char2 = loadImage("char2.png");
char22 = loadImage("char2-2.png");
char23 = loadImage("char2-3.png");
char3 = loadImage("char3.png");
char32 = loadImage("char3-2.png");
char33 = loadImage("char3-3.png");
char4 = loadImage("char4.png");
char42 = loadImage("char4-2.png");
char43 = loadImage("char4-3.png");
start = loadImage("startpage.png");
buttonnew = loadImage("buttonnew.png");
buttoncontinue = loadImage("buttoncontinue.png");
map1 = loadImage("map1.png");
}
void menu() {
float x1 = width/5;
float y1 = height/3;
float x2 = width/5*3.5;
float y2 = height/3;
int w = 120;
int h = 50;
image(start, 0, 0, width, height);
image(buttonnew, x1, y1);
image(buttoncontinue, x2, y2);
if (mousePressed && (mouseButton == LEFT)) {
if (mouseX>x1 && mouseX<x1+w && mouseY>y1 && mouseY<y1+h) {
state = 2;
}
if (mouseX>x2 && mouseX<x2+w && mouseY>y2 && mouseY<y2+h) {
state = 2.1;
}
}
}
// MAP STUFF
void map1() {
image(map1, width, height);
sprite();
charimage();
}
//CHARACTER STUFF
void sprite() {
if (wPressed == true) {
dir = 4;
locy --;
}
if (sPressed == true) {
dir = 1;
locy ++;
}
if (aPressed == true) {
dir = 2;
locx --;
}
if (dPressed == true) {
dir = 3;
locx ++;
}
}
void keyPressed() {
if (key=='w') {
wPressed=true;
}
if (key=='s') {
sPressed=true;
}
if (key=='a') {
aPressed=true;
}
if (key=='d') {
dPressed=true;
}
}
void keyReleased() {
if (key=='w') {
wPressed=false;
}
if (key=='s') {
sPressed=false;
}
if (key=='a') {
aPressed=false;
}
if (key=='d') {
dPressed=false;
}
}
void charimage() {
if (dir == 1) {
if (step == 1) {
image(char1, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 2) {
image(char12, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 3) {
image(char13, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
}
if (dir == 2) {
if (step == 1) {
image(char2, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 2) {
image(char22, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 3) {
image(char23, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
}
if (dir == 3) {
if (step == 1) {
image(char3, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 2) {
image(char32, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 3) {
image(char33, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
}
if (dir == 4) {
if (step == 1) {
image(char4, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 2) {
image(char42, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
else if (step == 3) {
image(char43, locx, locy);
if (time > oldtime+50) {
step ++;
oldtime = time;
}
}
}
}
void draw() {
background(0);
if (state == 1) {
menu();
}
if (state == 2) {
map1();
}
if (state == 2.1) {
println("WINNER");
}
}
Answers
you don't need the 'data/' in the filenames
And i still do not know how to make this show up properly?
highlight the code, hit C button. but i've done it now...
this wasn't really worth starting a new question for imo. 8)
ok i removed 'data' from the file names and it comes up with the same problem.
In line 71 you have
state = 2.1;
but if you look in draw() you only test for 1 and 2 so when state = 2.1 the screen will be black because nothing gets drawn.
Ok now how can i make it so the walking animations work, i posted my code idea, but it doesnt work. I UPDATED MY CODE IN THE question at the top, also it still doesnt load the 'map1' image. just a black screen. Also the functions i am talking about are only reguarding the state == 2; portion the 2.1 isnt supposed to do anything yet.
When you re-post your code, please highlight it and press CTRL+K. It will make it a lot easier to read. I have done this for you... and @koogs had to do it earlier...
Control K just switches windows the same way as alt tab, it doesnt do anything to the code.
Then use the C button above the text area.
At worst, use an external editor to indent the whole code with a tab (or four spaces).
See the sticky post of this forum...
BTW, it is strange that your computer handles a Ctrl+ shortcut globally, these shortcuts are generally application-scoped, it is like having Ctrl+C closing the current window!
Are there any error messages?
If not then the images have been loaded but are not being displayed which means there is some logic error in your code. Since we don't have the images we can't run the sketch so it is difficult to find the problem. Can you archive the sketch (Tools | Archive Sketch menu option) and put the zip file created somewhere where we can download it?
I replaced the images by dummy ones. If I click on the left button, I see the char image(s) on a black background. On the right button, the screen is indeed black, and the console fills with WINNER message...
Indeed, I feel there is more a logical issue than a loading image one...
Note that using a float for the state is unusual, and comparisons like == 2.1 are prone to fail, because of rounding issues...
Left button / dark background:
image(map1, width, height);
You just display the image beyond the bottom-right corner! Should be
image(map1, 0, 0, width, height);
Thank you, that solved the background image problem, also THIS IS NOT A FINISHED GAME DONT WORRY ABOUT THAT THE RIGHT BUTTON WILL DO. now the only thing left is why the character image does not animate like it should. I can make it animate but i am trying to slow it down so its not animateing 120x per second. Lines 87-232.
Also, this is going to be 2d rpg style, how would i make it so that the character doesnt walk off the screen and instead the screen follows the character to a certain degree? so that when you hit an invisable square that is around the center of the screen that it begins to push the screen with the character? Your guyses help is appreciated.