if语句不检查变量的数量是否增加(An if statement isn't checking if a variable has increased in amount or not)

编程入门 行业动态 更新时间:2024-10-20 03:54:17
if语句不检查变量的数量是否增加(An if statement isn't checking if a variable has increased in amount or not)

我在Flash开发中编写AS3编程。 我正在使用一个名为flashpunk的程序,它为类添加了多个预设,我可以使用它来更容易编程。 我有一个if语句,当一个变量等于1时,它应该添加一个新图形。如果用户在键盘上按下一个,那么该变量确实增加1.但是当变量增加1并且if语句应该检查时如果它是一个它不会添加一个像它所假设的新图形。 我确实在不同的类中有if语句和变量,我不确定我是否可以这样做,这是代码。 if语句不起作用是应该添加新background1的语句。

章节

package { import net.flashpunk.Entity; import net.flashpunk.World; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; public class Chapter extends World { public var mainmenu:MainMenu; public var background1:Background1; public function Chapter() { mainmenu = new MainMenu(); add(mainmenu); background1 = new Background1(); if(mainmenu.YesNo == 1) { add(background1); } } } }

主菜单

package { import flash.events.TimerEvent; import flash.utils.Timer; import net.flashpunk.Entity; import net.flashpunk.graphics.Image; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.FP; public class MainMenu extends Entity { [Embed(source = "net/MainScreen.png")] private const SPRITE1:Class; public var YesNo:int = 0 private var sprite1:Image = new Image(SPRITE1); public function MainMenu() { graphic = sprite1; sprite1.centerOrigin(); x = 200 y = 150 layer = 150 } override public function update():void { if (Input.pressed(Key.DIGIT_1)) { YesNo = YesNo + 1; } trace(YesNo); } } }

I am programming in AS3 on flash develop. I am using a program called flashpunk that adds in multiple presets for classes that i can use to make it easier to program. I have an if statement that is supposed to add a new graphic when a variable equals 1. If the user presses one on their keyboard then that variable does increase by 1. But when the variable increases by 1 and the if statement is supposed to check if it is one it doesn't add a new graphic like it's supposed too. I do have the if statement and the variable in different classes and i am not sure if i can do that, here is the code. The if statement that doesn't work is the one that is supposed to add a new background1.

Chapter

package { import net.flashpunk.Entity; import net.flashpunk.World; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; public class Chapter extends World { public var mainmenu:MainMenu; public var background1:Background1; public function Chapter() { mainmenu = new MainMenu(); add(mainmenu); background1 = new Background1(); if(mainmenu.YesNo == 1) { add(background1); } } } }

MainMenu

package { import flash.events.TimerEvent; import flash.utils.Timer; import net.flashpunk.Entity; import net.flashpunk.graphics.Image; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.FP; public class MainMenu extends Entity { [Embed(source = "net/MainScreen.png")] private const SPRITE1:Class; public var YesNo:int = 0 private var sprite1:Image = new Image(SPRITE1); public function MainMenu() { graphic = sprite1; sprite1.centerOrigin(); x = 200 y = 150 layer = 150 } override public function update():void { if (Input.pressed(Key.DIGIT_1)) { YesNo = YesNo + 1; } trace(YesNo); } } }

最满意答案

问题是您的代码是在World类构造函数中。 Flashpunk对游戏对象的想法是将实体添加到Worlds中。 添加后,World会在每个帧中运行update()方法,它也会运行每个Entity的update() 。 您的if构造函数中的语句将始终产生false,因为尚未执行更新。

如果我理解正确,您希望在用户按1后添加后台实体。您可以这样做:

// in MainMenu class override public function update():void { if (Input.pressed(Key.DIGIT_1)) { world.add(new Background1()); } }

如果您想要保留对此实体的引用,您可以:

override public function update():void { if (Input.pressed(Key.DIGIT_1)) { Chapter(world).background1 = new Background1(); // note the casting of World to Chapter world.add(Chapter(world).background1); } }

The problem is that your code is in a World class constructor. Flashpunk's idea of game objects is that Entities are added to Worlds. Once added, World runs update() method every frame in which it also runs every Entity's update() as well. Yours if statement in a constructor will always yield false as update wasn't yet executed.

If I understand correctly you want to add background entity after user will press 1. You can do it like this:

// in MainMenu class override public function update():void { if (Input.pressed(Key.DIGIT_1)) { world.add(new Background1()); } }

If you want to hold reference to this entity you could:

override public function update():void { if (Input.pressed(Key.DIGIT_1)) { Chapter(world).background1 = new Background1(); // note the casting of World to Chapter world.add(Chapter(world).background1); } }

更多推荐

本文发布于:2023-08-07 06:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   语句   数量   statement   amount

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!