【问题标题】:How to make a moving picture go off the screen and make it come back again如何让运动画面离开屏幕并让它再次出现
【发布时间】:2022-01-11 18:03:35
【问题描述】:

我是处理新手,我正在创建这个小游戏,这些棒子试图避开攻击者。我添加了一个移动的棍子人。它从底部到顶部,但是当它到达顶部时,它不会再次从底部返回,它只是消失了。我想知道我能做些什么来做出这种改变。 Picture for the stick man moving upThis is what happens when the stick man go off the screen they don't return back from the bottom


void setup()
{
  size(800,400);
  stick2 = new Stick(10, 200, 2);
  stick3 = new Stick(150,200, 3);
}

void draw()
{
  background(240);
  stick2.render();
  stick2.move();
  stick3.render();
  stick3.move();
}
class Stick
{
 //members?
 int x, y;
 int speedX = 2;
 int speedY = 2;
 int animationcounter = 0; //animation
 PImage img1, img2;
  
 //constructor - load images
 Stick(int x, int y, int speed)
 {
   
   this.x = x;
   this.y = y;
   this.speedX = speed; 
   img1 = loadImage("stick1.png");//loads from .pde source code directory
   img2 = loadImage("stick2.png");

 }
  void render()
 {
   //cycle through images, and back to image1
   
   if (animationcounter >0 & animationcounter <=8)
    { image(img1,this.x,this.y); }
     else if (animationcounter >8 & animationcounter <=20)
    {image(img2,this.x,this.y);  }
    
    else
    {
    image(img2,this.x,this.y);
    }
    
    animationcounter = animationcounter+1;
    if (animationcounter>32)
      animationcounter = 0;
 }
 void move()
 {
   this.y = this.y - speedY; //move rightwards
   if (this.y>height)
     this.y = +img1.height;
 }
}

【问题讨论】:

    标签: java processing


    【解决方案1】:

    问题是您实际上并没有检查火柴人是否在屏幕上方。 您正在检查它的 y 值是否高于高度,换句话说,它是否在屏幕下方。

    另外,如果图像在出现在另一侧之前完全离开屏幕,那就太好了。 为此,您的移动功能应如下所示

    void move()
    {
       this.y = this.y - speedY;
    
       //image goes above the screen
       if (this.y < 0 - img1.height) //subtract the image height so the image completely disappears before it moves down
         this.y = height + img1.height;
       //image goes below the screen
       if (this.y > height + img1.height)
         this.y = -img1.height;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多