如何使用动作脚本覆盖动态类中的函数?
这是一个剧本应用程序。
我有两节课:
public dynamic class Bullet extends Sprite {
public function update():void {
x += 5;
y += 5;
}
}
public class BulletFactory {
public function createFastBullet:Bullet {
var result:Bullet = new Bullet();
result.update = function() {
x += 10;
y += 10;
}
}
}
这基本上就是我想要做的。在动作脚本中实现这一目标的最佳方法是什么?
This is for a playbook app.
I have two classes:
public dynamic class Bullet extends Sprite {
public function update():void {
x += 5;
y += 5;
}
}
public class BulletFactory {
public function createFastBullet:Bullet {
var result:Bullet = new Bullet();
result.update = function() {
x += 10;
y += 10;
}
}
}
This is essentially what I'm trying to do. What's the best way to achieve this in actionscript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不这样做。动态是允许您添加任何尚未声明的临时属性,而不是覆盖已经声明的函数。动态不要与抽象混淆。
请尝试类似的操作 根据
您的喜好调整 speedX/speedY 的公共/私人可见性。
另一方面,如果您想尽可能从字面意义上“动态覆盖函数”,那么总是有这个(虽然很hacky但有效)。
然后,在您的子弹工厂中,您可以临时分配更新功能。请注意,这不太“安全”,因为更新不再具有设置的签名,因此您会失去所有类型安全的概念。在调用它之前,您还必须确保它存在。如果要避免编译器警告,则必须显式进行 null 检查。
You don't do it that way. Dynamic is to allow you to add any ad-hoc property that isn't already declared, not to override already declared functions. Dynamic is not to be confused with abstract.
Try something like this instead
Adjust the public/private visibility of speedX/speedY to your liking.
If on the other hand you want to "dynamically override a function" in as literal a sense as possible, there's always this (which is hacky but valid).
Then in your bullet factory you can assign the update function on an ad-hoc basis. Note that this is less "secure" as you lose all notion of type safety as update no longer has a set signature. You also have to make sure it exists before you call it. You must make the null check explicit if you want to avoid a compiler warning.
您可以使用接口、扩展和覆盖的组合来解决您的问题。
下面的内容不会编译,但应该为您提供一个起点。
You could solve your issue using a combination of Interfaces, extensions and overrides.
The below won't compile, but should give you a starting point.