显示对象的宽度/高度的有趣问题
我在制作乒乓球/打砖块游戏时遇到了一个奇怪的问题:玩家的特殊能力之一是使第二个球从主球中分离出来,并在短时间内与主球完全相同消失前的一段时间。问题在于,当第二个球与阶段边界接触并且应该简单地反转其 x 或 y 速度(取决于边界击中)时,它也会由于某些奇怪的原因而明显增加其宽度和高度属性。图形不会改变,但宽度和高度的值会(极大地)改变,这会导致我为奇怪的情况定义了一些卡住处理程序方法,其中球最终部分离开舞台被重复调用。我在代码中没有任何地方更改宽度、高度或scaleX/scaleY 属性,所以我不知道为什么会发生这种情况。在阅读 adobe 文档时,我发现以下两点与此问题有关 -
关键字 THIS:对方法包含对象的引用。当脚本执行时,“this”关键字引用包含该脚本的对象。在方法体内,“this”关键字引用包含被调用方法的类实例。 ---这不是同一件事的两种说法吗?我不太明白脚本开头的“this”和方法内的“this”之间的范围差异的细节...由于第二个球对象是在游戏过程中动态创建的(主球是静态对象)在运行前创建并通过不同的驱动程序类 Ball 进行控制),并且实际上链接到控制和构造它的 Ball2 类,“this”关键字在整个类的不同位置大量使用来引用这些图形。我担心对 AS3 中围绕“this”的细微差别的一些混淆可能是我所看到的奇怪情况的部分原因。
宽度/高度属性:指示显示对象的宽度/高度,以像素为单位。宽度/高度是根据显示对象内容的边界计算的。当您设置 width/height 属性时,scaleX/scaleY 属性也会相应调整。 ---“显示对象的内容”到底是什么意思?这仅仅是图形还是在某些情况下可能是其他东西?由于某种原因速度反转后内容会发生变化吗?通过调试打印语句,我发现在宽度和高度扩展时,scaleX和scaleY属性保持为1,这表明Flash不“知道”对象的大小有效改变...
我已附上输出日志它显示了与第二个球的位置、速度和大小相关的许多属性,以及它们如何随时间变化。您可以看到,宽度和高度属性在速度反转后急剧增加,这表明与边界发生碰撞。另外值得注意的是,第二个球是作为主文档类的子级创建的(没有对其应用任何转换,并且实际上从未添加到显示列表中),并且它似乎在正确的舞台全局坐标之外进行操作系统。
I've run into an odd problem in a pong/brick-breaker game I'm making: one of the player's special abilities is to make a second ball split off from the main one and act exactly as the main one does for a short period before vanishing. The trouble is that when the second ball comes into contact with a stage boundary and is supposed to simply reverse its x or y velocity (depending on the boundary hit), it also apparently increases its width and height properties for some odd reason. The graphic doesn't change, but the values of width and height do (drastically) and this results in some stuckHandler methods I've defined for the odd situation where a ball ends up partially offstage getting called repeatedly. Nowhere in the code do I change width, height, or scaleX/scaleY properties, so I have no idea why this is happening. In reading the adobe documentation I found the following two points to be of potential interest regarding this matter--
Keyword THIS: A reference to a method's containing object. When a script executes, the 'this' keyword references the object that contains the script. Inside a method body, the 'this' keyword references the class instance that contains the called method.
---Isn't this two ways to say the same thing? I don't quite understand the particulars of the scope difference between 'this' at the start of a script and 'this' inside a method... Since the second ball object is created dynamically during play (the primary ball is a static object created pre-runtime and is controlled via a distinct driver class Ball) and is actually linked to a class Ball2 which controls and constructs it, the 'this' keyword is used a lot in different places throughout the class to reference thew graphic. I fear some confusion over the nuances surrounding 'this' in AS3 may be partly to blame for the odd circumstances I'm seeing.Width/Height properties: Indicates the width/height of the display object, in pixels. The width/height is calculated based on the bounds of the content of the display object. When you set the width/height property, the scaleX/scaleY property is adjusted accordingly.
---What exactly do they mean by 'content of the display object'? Is that just the graphic or could it be something else under certain circumstances? Could the content change after a velocity reversal for some reason? Via debugging print statements, I found that at the point where the width and height expand the scaleX and scaleY properties remain at 1, indicating that Flash doesn't 'know' the object's size effectively changed...
I've attached the output log which shows a number of properties pertinent to the second ball's position, velocity, and size, and how they change over time. You can see that the width and height properties increase dramatically just after a velocity reversal, which indicates collision with a boundary. Also of note is that the second ball is created as a child of the main document class (which has no transformations applied to it, and is actually never added to the display list) and it seems to operate off of the proper stage-global coordinate system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的 Ball2 类中:
您正在向球添加某种大小的孩子。难怪它的尺寸增大了。 DisplayObject 始终足够大以包含其子项和/或图形形状。您确定您的对象嵌套正确吗?
From your Ball2 class:
You're adding child of some size to ball. No wonder that its size is increased. DisplayObject is always big enough to contain its children and/or graphic shapes. Are you sure your objects are nested properly?