我正在处理方面做一个简短的游戏,尽管如果我让它运行太长时间,我会遇到程序崩溃的问题。当我打开程序时,内存缓慢增加到800 MB,然后接近900 MB时,它死掉并慢慢退回。每当它达到500 MB左右时,程序就会崩溃。我对使用Processing非常陌生,所以我可能只是忘记了一些东西而计数器失控了,但是我似乎无法找到问题。谢谢阅读。 (文件中的所有图像均为占位符)
关于这个问题,我还无法弄清楚,我已经研究了“ System.gc();”。但是我不确定这是否是问题的一部分,以及如何实现这一点,就像我已经尝试过的那样。
//Imports
import processing.sound.*;
//Global variables
int gameState = 0; //Using an integer value just in case I want to add more maps in future updates
int backX = 1800; //Current background x value
float theta = 0.0; //Used to control sin wave for oscillation
int menuCounter = 0; //Main menu space bar counter
int menuSpaceSize = 25; //Space text size
int menuSpaceX = 90; //Space text menu
float menuSpaceModifier = 1.0; //Allows for space text movement
int mainMenuX = 50; //Main menu x position
color spaceColor = color(255); //Color the space text is (used for color changes on space press)
boolean[] keys = new boolean[4]; //WASD keys
PImage backImage; //Images
SoundFile select, bg, menubg; //Sound files
boolean bgPlay, menuPlay = false; //Music Playing boolean
//Setup function (Runs once)
void setup() {
//Set frame name
surface.setTitle("Game");
//Size
size(600, 600, P3D);
//Load images
backImage = loadImage("https://github.com/Attaxika/stuff/blob/master/BackgroundImage.png?raw=true");
//Load sound files
select = new SoundFile(this, "https://raw.githubusercontent.com/Attaxika/stuff/master/select.wav?raw=true");
bg = new SoundFile(this, "https://raw.githubusercontent.com/Attaxika/stuff/master/bg.mp3?raw=true");
menubg = new SoundFile(this, "https://raw.githubusercontent.com/Attaxika/stuff/master/menu.wav?raw=true");
}
//Draw function (Runs 60 times a second)
void draw() {
//Reset opacity
tint(255, 255);
//Reset color
fill(0);
//Make smooth
smooth();
//Background
background(0, 0, 0);
//Framerate set
frameRate(60);
//If the game isn't running
if(gameState == 0) {
//Stop background music
bg.stop();
bgPlay = false;
//Start menu music
if(!menuPlay) {
menuPlay = true;
menubg.amp(1);
menubg.loop();
}
//Main menu
textSize(50);
//y value for main menu text oscillation
float y = (sin(theta) + 1 ) * 20;
//Increment theta
theta += 0.05;
//Primary game menu
fill(255, 0, 0);
text("Game Menu", width/2 - 150, y + 75);
//4th fill value represents alpha
fill(255, 255, 255, 25);
//Shadow Text
text("Game Menu", width/2 - 155, y + 74);
//Space to start text
fill(spaceColor);
textSize(menuSpaceSize);
text("Space to Start", (width/2 - menuSpaceX) + menuSpaceModifier, 200);
//Check for space pressed
if(menuCounter > 0 && menuCounter < 30) {
menuCounter += 1;
menuSpaceSize = 30;
menuSpaceX = 140;
menuSpaceModifier += 20;
}
//Check for space timer
if(menuCounter == 29) {
gameState = 1;
}
}
//If the game is running
else if(gameState == 1) {
//Music start
if(!bgPlay) {
//Stop menu music
menuPlay = false;
menubg.stop();
bgPlay = true;
bg.amp(.40);
bg.loop();
}
//Background image
imageMode(CENTER);
image(backImage, backX, backImage.height / 2, backImage.width, backImage.height);
backX -= 1;
//Reset background x if it gets to the end
if(backX <= 0) {
backX = 1800;
}
}
//Unknown gameState
else {
exit();
}
}
//Key pressing (Keyboard input)
void keyPressed() {
//Pressed space & is on menu
if(key == ' ' && gameState == 0 && menuCounter == 0) {
spaceColor = color(0, 255, 0);
menuCounter += 1;
select.play();
bgPlay = true;
}
}
预期结果是它可以长时间运行,而不会占用内存,直到崩溃。
编辑: 有人回复了,但删除了他们的回复,但是我遵循了一些指示,但无济于事。我在程序中添加了以下代码,菜单屏幕现在无限期运行且内存使用率低,但是进入实际游戏(gameState = 1)导致游戏崩溃,并指出它在“ image(backImage,backX,backImage)处存在NullPointerException .height / 2,backImage.width,backImage.height);”
这是我在“ void draw(){”之后直接添加的内容
clear();
if(backImage!=null) {
image(backImage, backX, backImage.height / 2, backImage.width, backImage.height);
}
if(millis() > 20000 && backImage!=null) {
g.removeCache(backImage);
backImage = null;
}
System.gc();
答案 0 :(得分:2)
仔细检查我的代码并查看了一些人的建议,发现与多次渲染且从未清除过的图像或声音文件无关,我发现问题来了从我使用P3D渲染器。我从size()中删除了“ P3D”;然后我的程序内存使用量稳定在200 MB以下,并且从未激增。 (据我所知,如果您不想使用默认的2D渲染器,先使用形状然后在该3D形状上应用纹理是一种可能的解决方案)