我开始使用ActionScript编写代码,并尝试执行此程序。它在舞台上绘制一个形状,您可以使用箭头键移动它。我添加了一个“边缘粘贴”功能,将一半的形状粘在边缘上。这是我的代码:
function freemove(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
testing.y -= 5;
if(testing.y < stage.width)
{
testing.y = 0;
}
break;
}
case Keyboard.DOWN:
{
testing.y += 5;
// FOR BOTTOM EDGE.
break;
}
case Keyboard.LEFT:
{
testing.x -= 5;
if(testing.x < stage.height)
{
testing.x = 0;
}
break;
}
case Keyboard.RIGHT:
{
testing.x += 5;
// FOR RIGHT EDGE.
break;
}
}
}
问题是:它只适用于左边和上边。如何使其适用于底部和右边缘?谢谢! =)
答案 0 :(得分:2)
// FOR BOTTOM EDGE.
if (shape.y + shape.height > stage.stageHeight)
// FOR RIGHT EDGE.
if (shape.x + shape.width > stage.stageWidth)
另外,您可能会在LEFT和RIGHT处理程序中混淆宽度和高度(为什么y
与宽度进行比较,x
与高度进行比较?)