简体   繁体   English

如何让动图 go 掉屏并让它重新出现

[英]How to make a moving picture go off the screen and make it come back again

I am new to processing and I am creating this small game where these stick man try to avoid the attacker.我是处理新手,我正在创建这个小游戏,这些火柴人试图避开攻击者。 I have added a moving stick man it goes from bottom to top, but when it reaches the top it doesnt come back from down to up again it just disappears.我添加了一个移动的棍子人,它从下到上移动,但是当它到达顶部时,它不再从下到上返回,它只是消失了。 I was wondering what I could do to make this change.我想知道我能做些什么来做出这种改变。 Picture for the stick man moving up This is what happens when the stick man go off the screen they don't return back from the bottom Code picture 1 Code picture 2火柴人向上移动的图片这是当火柴人 go 离开屏幕时他们没有从底部返回时发生的情况代码图片 1代码图片 2


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;
 }
}

The problem is that you're not actually checking if the stickman goes above the screen.问题是您实际上并没有检查火柴人是否在屏幕上方。 You're checking if its y value is higher than height, in other words, if it's below the screen.您正在检查它的 y 值是否高于高度,换句话说,它是否在屏幕下方

屏幕坐标示例

Also, it would be nice if the image goes completely off the screen before it appears on the other side.此外,如果图像在出现在另一侧之前完全离开屏幕,那就太好了。 To achieve this, your move function should look like this为此,您的移动 function 应如下所示

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;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用viewHolder使listView用户输入在屏幕滚动时不返回默认值 - How to use viewHolder to make listView user input not go back to default when scrolling off screen 如何使EditText具有一定的最大宽度,但不离开屏幕? - How do I make an EditText have a certain maximum width, but not go off the screen? 如何在Java中使图形从屏幕上消失? - How to make graphics disappear off the screen in java? 如何使keyListener返回上一个键 - How to make a keyListener go back to previous key 如何使随机生成的对象出现在屏幕上? - How do I make Random generating objects come on my Screen? 回到相机之前,如何使应用显示图片5秒钟? - How to make app display picture for 5 seconds before going back to camera? 当玩家移动到屏幕边缘时,如何让背景“切换”,然后将它们传送回来,就像在一个世界中移动一样 - How do I make it so the background “switches” when the player moves to the edge of the screen, and teleports them back, as if moving through a world 如何使WebView片段内部返回上一个选项卡? - How to make WebView inside fragment to go back to previous tab? 如何让应用程序等待然后开始活动或返回? - How to make app wait and then start activity or go back? 如何使该程序返回提示,直到用户退出 - How to make this program go back to prompt until the user exits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM