火箭穿过 class。加工
Rocket move through class. Processing
我有一个动态图像作为背景
PImage background;
int x=0; //global variable background location
rocket Rocket;
void setup(){
size(800,400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
Rocket = new rocket();
}
void draw ()
{
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -=4;
if(x == -background.width)
x=0; //wrap background
Rocket.defender();
Rocket.move();
}
在另一个 class 我正在尝试让火箭上下移动
class rocket {
float x;
float y;
float speedy;
boolean up;
boolean down;
rocket(){
x = 50;
y = 200;
speedy = 3;
}
void move() {
if(up)
{
y = y - speedy;
}
if(down)
{
y = y + speedy;
}
}
void defender(){
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,100);
rect(x,y-10,20,10);
}
void keyPressed(){
if(keyCode == UP)
{
up = true;
}
if(keyCode == DOWN)
{
down = true;
}
}
void keyReleased(){
if(keyCode == UP)
{
up = false;
}
if(keyCode == DOWN)
{
down = false;
}
}
}
火箭会显示但不会移动。我尝试了我所知道的一切,但没有任何效果。我也尝试了火箭 class 就像一个项目本身和火箭移动,所以它必须是 class 的东西。我对编码很陌生,所以请记住这一点,提前谢谢你。
keyPressed()
和 keyReleased()
函数(以及任何其他事件函数)需要处于草图级别,而不是在另一个 class 中。如果它们在另一个 class 中,Processing 不知道如何找到它们。
因此,您需要做的是将 keyPressed()
和 keyReleased()
函数移动到草图中,然后调用 Rocket
class 上的函数(class 名称应以大写字母开头,顺便说一句),类似于您从草图中调用 rocket.defender()
和 rocket.move()
(变量名称应以小写字母开头)的方式-级别 draw()
函数。
我有一个动态图像作为背景
PImage background;
int x=0; //global variable background location
rocket Rocket;
void setup(){
size(800,400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
Rocket = new rocket();
}
void draw ()
{
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -=4;
if(x == -background.width)
x=0; //wrap background
Rocket.defender();
Rocket.move();
}
在另一个 class 我正在尝试让火箭上下移动
class rocket {
float x;
float y;
float speedy;
boolean up;
boolean down;
rocket(){
x = 50;
y = 200;
speedy = 3;
}
void move() {
if(up)
{
y = y - speedy;
}
if(down)
{
y = y + speedy;
}
}
void defender(){
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,100);
rect(x,y-10,20,10);
}
void keyPressed(){
if(keyCode == UP)
{
up = true;
}
if(keyCode == DOWN)
{
down = true;
}
}
void keyReleased(){
if(keyCode == UP)
{
up = false;
}
if(keyCode == DOWN)
{
down = false;
}
}
}
火箭会显示但不会移动。我尝试了我所知道的一切,但没有任何效果。我也尝试了火箭 class 就像一个项目本身和火箭移动,所以它必须是 class 的东西。我对编码很陌生,所以请记住这一点,提前谢谢你。
keyPressed()
和 keyReleased()
函数(以及任何其他事件函数)需要处于草图级别,而不是在另一个 class 中。如果它们在另一个 class 中,Processing 不知道如何找到它们。
因此,您需要做的是将 keyPressed()
和 keyReleased()
函数移动到草图中,然后调用 Rocket
class 上的函数(class 名称应以大写字母开头,顺便说一句),类似于您从草图中调用 rocket.defender()
和 rocket.move()
(变量名称应以小写字母开头)的方式-级别 draw()
函数。