我试图根据条件使许多相同的对象随机出现在屏幕上并不断出现错误?
一个好朋友向我推荐了这个网站,看起来真的很有用!我对动作脚本有点无耻的菜鸟,经过 3 天的教程和建议,我遇到了障碍。
我已经设法使用一种名为 AS3glue 的东西将传感器连接到 arduino 与闪存通信。它有效,当我为传感器读取 0 的条件设置跟踪(“叶子”)时,我得到“叶子”一词的打印输出。但是,我希望程序在满足此条件时使图形出现在屏幕上,而不仅仅是跟踪某些内容。
我试图让程序在每次满足条件时在屏幕上的随机位置生成一个名为“Enemy”的库对象。它被称为敌人是因为我正在学习游戏教程......实际上它是一片叶子的图画。
这是给我带来问题的代码:
var 军队:数组; var 敌人:敌人;
函数AvoiderGame() { 军队= 新数组(); var newEnemy = 新 敌人( 100, 100 );军队.推( 新敌人); addChild( 新敌人 ); }函数timerEvent(事件:事件):void {
if (a.getAnalogData(0) ==0 && a.getAnalogData(0) != this.lastposition){
跟踪(“叶子”); var randomX:Number = (Math.random() * 200) + 100; var randomY:Number = (Math.random() * 150) + 50; var newEnemy = 新敌人( randomX, randomY); 军队.push( newEnemy ); addChild( 新敌人 ); } else if (a.getAnalogData(0) == 0) { //不跟踪任何内容 } >else {
//不跟踪任何东西 } this.lastposition = a.getAnalogData(0); //之后,设置 成为新人的位置 最后位置并重复。
}
已经导入了“导入 flash.display.MovieClip;”
并且 Enemy 类的代码看起来 像这样:
<块引用>包{导入 flash.display.MovieClip;公开课 敌人扩展了 MovieClip { public 函数 Enemy( startX:数字, 起始Y:数字 ) { x = 起始X; y = 开始Y; } } }
这是我的错误 我尝试过谷歌搜索,这似乎是一个非常普遍的错误:
TypeError:错误#1009:无法访问 空对象的属性或方法 参考。在 as3glue_program_fla::MainTimeline/timerEvent() 在 flash.utils::Timer/_timerDispatch() 在 flash.utils::Timer/tick()
我已确保为 AS3 导出“Enemy”对象。
当它在 AS2 中编程时,我想要这样的东西:
叶计数器 = 0; //将计数器设置为 0 计数器.swapDepths(1000); //把 柜台几乎在上面 任何事情,除非你赚的钱超过 1000片叶子! counter.textbox.text = 0; //在文本框中显示“0” “计数器”影片剪辑
this.onMouseDown = function() { //鼠标点击时触发 this.attachMovie("叶子","叶子"+leafCounter,leafCounter,{_x:Math.random()*Stage.width,_y:Math.random()*Stage.height,_rotation:Math.random()*360 }); //用a向舞台添加一片叶子 随机位置和随机旋转 叶计数器++; //给叶子加1 计数器 counter.textbox.text = 叶计数器; //显示该数字 文本框}
但我无法让它生成“敌人”
任何帮助或提示都会非常有用!我知道改变现有代码有点笨拙。
A good friend recommended this site to me, it looks really useful! I'm a bit of a shameless noob at actionscript and after 3 days of tutorials and advice I've hit a brick wall.
I've managed to get a sensor attached to an arduino talking to flash using something called AS3glue. it works, when i set up a trace("leaf") for the contition that the sensor reads 0, i get a printout of the word "leaf". however i want the program to make a graphic appear on the screen when this condition is met, not just trace something.
I'm trying to get the program to generate a library object called "Enemy" on the screen at a random position each time the conditions are met. It's called enemy because I was following a game tutorial...actually it's a drawing of a leaf.
Here's the bit of the code which is causing me problems:
var army:Array; var enemy:Enemy;
function AvoiderGame() { army =
new Array(); var newEnemy = new
Enemy( 100, 100 ); army.push(
newEnemy ); addChild( newEnemy );
}function timerEvent(event:Event):void
{if (a.getAnalogData(0) ==0 &&
a.getAnalogData(0) !=
this.lastposition){trace("leaf"); var randomX:Number = (Math.random() * 200) + 100; var randomY:Number = (Math.random() * 150) + 50; var newEnemy = new Enemy( randomX, randomY); army.push( newEnemy ); addChild( newEnemy ); } else if (a.getAnalogData(0) == 0) { //don't trace anything } >else {
//don't trace anything }
this.lastposition =
a.getAnalogData(0); //afterwards, set
the position to be the new
lastposition and repeat.}
I've imported "import
flash.display.MovieClip;"
and the code for the Enemy class looks
like this:
package { import
flash.display.MovieClip; public class
Enemy extends MovieClip { public
function Enemy( startX:Number,
startY:Number ) { x = startX;
y = startY; }
} }
Here's my error. I've tried googling, it seems like a pretty general error:
TypeError: Error #1009: Cannot access
a property or method of a null object
reference. at
as3glue_program_fla::MainTimeline/timerEvent()
at
flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
I've made sure that the "Enemy" object is exported for AS3.
I'm going for something like this when it's programmed in AS2:
leafCounter = 0; //set the counter to
0 counter.swapDepths(1000); //puts the
counter on top of pretty much
anything, unless you make more than
1000 leaves! counter.textbox.text = 0;
//shows "0" in the text box in the
"counter" movie clipthis.onMouseDown = function() {
//triggers when the mouse is clicked
this.attachMovie("Leaf","Leaf"+leafCounter,leafCounter,{_x:Math.random()*Stage.width,_y:Math.random()*Stage.height,_rotation:Math.random()*360});
//adds a leaf to rthe stage with a
random position and random rotation
leafCounter++; //adds 1 to the leaf
counter counter.textbox.text =
leafCounter; //shows that number in
the text box }
I'm sure it must be a simple error, I can get the logic working when it just traces something on the screen but i can't get it to generate an "enemy"
Any help or hints would be really useful! I know this is a bit of a ham-fisted job of altering existing code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,您抛出的错误意味着某些东西在您认为应该存在时并不存在。目前,最好的候选人正在寻找最后的位置,尽管您可能之前已经举例过。注释掉每个全局变量直到它起作用。您似乎正在检查 a.input 是否等于 0 但不等于 lastposition,然后您稍后将 lastposition 设置为 a.input (必须为 0)也检查一下。
ok, the error you are throwing means something does not exist when you think it should. At the moment the best candidate is looking to be lastposition, though you might have instanced it earlier. Comment out each global var til it works. You seem to be checking that a.input is equal to 0 but not lastposition, then you later set lastposition as a.input (which must be 0) check that too.
由于某种原因,取出了 Army.push( newEnemy ); 行使这个工作完美! :D
For some reason, taking out the line army.push( newEnemy ); makes this work perfectly! :D