2012-07-27 31 views
0

在标准绘制中有一个isKeyPressed()功能。该函数返回一个boolean,指示参数是否被按下。标准绘制是按键跳过舞台

在我制作的基于文本的游戏中,用户将按yn确定结果。有时但并非总是如此,下面的故事文本被跳过。这是我的代码。我在日食中运行这个。这不是我的全部代码,但我认为问题在哪里。

while(true){ 

else if(stage == 6){ 

StdDraw.textLeft(0, 700,"'so, ya name's "+ name + ", huh?'"); 

StdDraw.textLeft(0, 680,"'What an odd name if i do say so.'"); 

StdDraw.textLeft(0, 660,"'you're lucky i found you when i did, the dark forest is dangerous!'"); 

StdDraw.textLeft(0, 620,"'So, you heading to Ivangard, right?'(y/n)"); 
      counter = 1; 

if(StdDraw.isKeyPressed('Y')&&frame%8==0){ 

stage = 7; 

      } 
else if (StdDraw.isKeyPressed('N')&&frame%8==0){ 
        stage = 9; 
      } 
     } 
else if(stage == 7){ 

StdDraw.textLeft(0, 700,"'well lucky you! i'm going in the same direction!'"); 

    StdDraw.textLeft(0, 680,"'Well i'm Program "+ random +", nice to meet you.'"); 

    StdDraw.textLeft(0, 660,"'take this rusty dagger. may be of use!'"); 

    StdDraw.textLeft(0, 640,"'ill see you in Ivangaurd!'"); 

        StdDraw.textLeft(0, 500,"continue?(O)"); 

        if(StdDraw.isKeyPressed('O')&&frame%12==0){ 

         stage = 11; 

         programn = random; 

         fist = 0; 

         rustyd = 1; 

       } 

       } 



      else if(stage == 9){ 

     StdDraw.textLeft(0, 700,"'Well, if you need me, ill be in Ivangaurd'"); 

     StdDraw.textLeft(0, 680,"'I'm Program #"+ random +", nice to meet you.'"); 

     StdDraw.textLeft(0, 660,"'take this rusty dagger. may be of use!'"); 

       StdDraw.textLeft(0, 620,"continue?(O)"); 

       counter = 1; 

       programn = random; 

       fist = 0; 

       rustyd = 1; 



       if(StdDraw.isKeyPressed('O')&&frame%12==0){ 

        stage = 11; 

      } 

      } 

else if(stage == 11){ 

     StdDraw.textLeft(0, 700,"Part 2: The virus"); 

    StdDraw.textLeft(0, 660,"as you leave the dark forest, The voice comes back"); 

    StdDraw.textLeft(0, 640,"A great evil is coming to this land."); 

     StdDraw.textLeft(0, 620,"a virus, a plague the program..."); 

     StdDraw.textLeft(0, 600,"unlike the program, all the virus does is eat."); 

       StdDraw.textLeft(0, 580,"That is why we need you."); 

StdDraw.textLeft(0, 560,"you are human, uneatable to the virus. All other warriors would fall were you stand."); 

       StdDraw.textLeft(0, 540,"continue?(0)"); 

       if(StdDraw.isKeyPressed('O')&&frame%12==0){ 

        stage = 10; 

       } 

      } 

} 

问题出在y/n选项之后,它跳过下面的文本。

新的文字在这里。我是新手编码器,所以我可能不知道你告诉我要做什么。

else if(stage == 6){ 
      StdDraw.textLeft(0, 700,"'so, ya name's "+ name + ", huh?'"); 
      StdDraw.textLeft(0, 680,"'What an odd name if i do say so.'"); 
      StdDraw.textLeft(0, 660,"'you're lucky i found you when i did, the dark forest is dangerous!'"); 
      StdDraw.textLeft(0, 620,"'So, you heading to Ivangard, right?'(y/n)"); 
      counter = 1; 
      if(StdDraw.isKeyPressed('Y')&&frame%8==0){ 
       stage = 7; 

      } 
      if(StdDraw.isKeyPressed('N')&&frame%8==0){ 
       stage = 9; 
      } 
     } 
+0

而问题是? – MadProgrammer 2012-07-27 02:39:14

+0

问题出在y/n选项之后,它跳过以下文本。 – user1507910 2012-07-27 02:46:28

+0

你是否从编译器收到任何错误或警告? – 2012-07-27 04:35:40

回答

1

好的,您试图更改else-if语句的执行路径。你不能这样做。

if (animal == sheep) { 

    System.out.println("Fox in a sheep skin"); 
    animal = fox; 

} else if (animal == fox) { 

    System.out.println("hide the sheep"); 

} 

现在基本上,如果ELSE执行animal == sheep那么第一条语句,如果执行animal == fox然后第二条语句。你不能改变侧面的流程,因为它已经决定了执行的路径。

这就像未来的岔路口,你只能走一个方向,而不是两个......

if (animal == sheep) { 

    System.out.println("Fox in a sheep skin"); 
    animal = fox; 

} 

if (animal == fox) { 

    System.out.println("hide the sheep"); 

} 

会做你想要什么,因为它的条件需要检查它更贵,但它强制执行每个语句的流程。

UPDATE

我怀疑你误解在哪里进行更改(没有任何证据说,否则,就很难知道)

这是它应该是什么样子(我带出来的文字声明使其更易于阅读)

if(stage == 6){ 

    //... some text 

    if(StdDraw.isKeyPressed('Y')&&frame%8==0){ 
     stage = 7; 
    } else if (StdDraw.isKeyPressed('N')&&frame%8==0){ 
     stage = 9; 
    } 
} 

if(stage == 7){ 

    //... some text 

    if(StdDraw.isKeyPressed('O')&&frame%12==0){ 

     stage = 11; 

     programn = random; 

     fist = 0; 

     rustyd = 1; 

    } 

} 

if(stage == 9) { 

    //... some text 

    if(StdDraw.isKeyPressed('O')&&frame%12==0){ 

     stage = 11; 

    } 

} 

if(stage == 11){ 

    //... some text 

    if(StdDraw.isKeyPressed('O')&&frame%12==0){ 

     stage = 10; 

    } 

} 
+0

更改为if语句。为什么它不起作用? – user1507910 2012-07-27 04:19:56

+0

您是否善于用您的新代码更新您的示例,以便我可以对您的做法有一些了解,请 – MadProgrammer 2012-07-27 04:21:17

+0

更改为if语句。为什么它不起作用?我增加了帧延迟,下一位文本等待帧延迟,然后进入下一位文本而不输入'O'。 – user1507910 2012-07-27 04:25:55

2

StdDraw.isKeyPressed()发生在一个int不是char

public static boolean isKeyPressed(int keycode) 

当前是否被按下的键码返回true,否则为false

StdDraw.isKeyPressed

的键码的说明,请参见KeyEvent.java

更新:的任择议定书要求为例:

if(StdDraw.isKeyPressed(KeyEvent.VK_Y) { 
    // True if the 'y' key is currently being pressed 
} 
+0

你能举个例子吗? – user1507910 2012-07-27 04:42:23

+0

@ user1507910:答案已更新 – 2012-07-27 04:45:37

+0

VK_Y出现错误。难道我做错了什么? – user1507910 2012-07-27 04:47:58