Indexing 错误#2006:提供的索引超出范围
我一直在Indexing 错误#2006:提供的索引超出范围,indexing,bounds,Indexing,Bounds,我一直在 Error #2006: The supplied index is out of bounds. at flash.display::DisplayObjectContainer/getChildAt() at Main/onFrame() 这主要是指我的代码的这一部分 else if (comp) //if completion is true { var animation = char.getChildAt(2); // if (ani
Error #2006: The supplied index is out of bounds.
at flash.display::DisplayObjectContainer/getChildAt()
at Main/onFrame()
这主要是指我的代码的这一部分
else if (comp) //if completion is true
{
var animation = char.getChildAt(2); //
if (animation.currentFrame == animation.totalFrames)
{
animation.stop();
addChild(end);
我在第二帧上绘制的动画也没有运行,尽管我已经检查了符号和其中的帧,它应该可以正常工作。我对代码非常陌生,这就是我到目前为止所学的
这是我的代码的其余部分。
我们应该制作一个基本的游戏,在这个游戏中,我们的角色走到一个通电的地方,做一个通电的动画,然后是一个游戏的结束标题
package
{
import flash.display.MovieClip;
import fl.motion.easing.Back;
import flash.sampler.Sample;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip
{
var bg:Background;
var b:Bubbles;
var b2:Bubbles;
var s:Seaweed;
var pressingRight:Boolean = false;
var pressingLeft:Boolean = false;
var comp:Boolean = false;
var speed:int = 10;
var char:Character;
var pu:PowerUp;
var hit:hit1
var end:EndGame;
public function Main()
{
bg = new Background;
addChild(bg);
char = new Character();
addChild(char);
char.x = stage.stageWidth/2;
char.y = 488;
b = new Bubbles();
addChild(b);
b2 = new Bubbles();
addChild(b2);
b2.y = +b2.height;
s = new Seaweed();
addChild(s);
pu = new PowerUp();
addChild(pu);
pu.x = 200;
pu.y = 450;
pu.height = 50;
pu.scaleX = pu.scaleY;
pu.gotoAndStop("SPIN");
hit = new hit1;
addChild(hit);
hit.x = char.x
hit.y = char.y - 50
end = new EndGame();
end.x = stage.stageWidth/2;
end.y = stage.stageHeight/2;
stage.addEventListener(Event.ENTER_FRAME, onFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}//main()
public function onFrame(e:Event)
{
if (!comp)
//Bubble Movement
b.y -= 1;
b2.y -= 1;
if (b.y >= stage.stageHeight)
{
b.y = b2.y + bg.height;
}
else if (b2.y >= stage.stageHeight)
{
b2.y = b.y + b2.height;
}
//Background & Character Movement
if (pressingRight && char.x < stage.stageWidth/2)
{
char.x += speed * 0.4
}
else if (pressingRight == true && (bg.width + bg.x) > stage.stageWidth)
{
bg.x -= speed * 0.4;
s.x -= speed * 0.6;
pu.x -= speed * 0.4;
}
else if (pressingRight == true)
{
char.x += speed * 0.4;
}
if (pressingLeft == true && char.x > stage.stageWidth/2)
{
char.x -= speed * 0.4;
}
else if (pressingLeft == true && bg.x <0)
{
bg.x += speed * 0.4;
s.x += speed * 0.6;
pu.x += speed * 0.4;
}
else if (pressingLeft)
{
char.x -= speed * 0.4;
}
//Boundaries
if (char.x > stage.stageWidth)
{
char.x = stage.stageWidth;
}
else if (char.x < 0)
{
char.x = 0;
}
//Character Looking Directions
if (pressingLeft == true)
{
char.scaleX = -1;
hit.x = char.x
}
if (pressingRight == true)
{
char.scaleX = 1;
hit.x = char.x
}
//Character Movements
if (pressingRight || pressingLeft)
{
char.gotoAndStop("WALK");
}
else if (!pressingRight || !pressingLeft)
{
char.gotoAndStop("IDLE");
}
//Getting the Power up
if (pu.hitTestObject(hit))
{
char.gotoAndStop("POWER");
comp = true;
pu.gotoAndStop("GONE");
}
// !end
else if (comp) //if completion is true
{
var animation = char.getChildAt(2); //
if (animation.currentFrame == animation.totalFrames)
{
animation.stop();
addChild(end);
}
}//Comp
}//onFrame
public function keyPressed(k:KeyboardEvent)
{
if (k.keyCode == Keyboard.RIGHT)
{
pressingRight = true;
}
else if (k.keyCode == Keyboard.LEFT)
{
pressingLeft = true;
}
} // keyPressed()
public function keyReleased(k:KeyboardEvent)
{
if (k.keyCode == Keyboard.RIGHT)
{
pressingRight = false;
}
else if (k.keyCode == Keyboard.LEFT)
{
pressingLeft = false;
}
} // keyReleased()
}//public class()
}//package()
包
{
导入flash.display.MovieClip;
导入fl.motion.easing.Back;
进口flash.sampler.Sample;
导入flash.events.Event;
导入flash.events.KeyboardEvent;
导入flash.ui.Keyboard;
公共类Main扩展了MovieClip
{
var-bg:背景;
var b:泡沫;
var b2:气泡;
变种s:海藻;
var pressingRight:布尔值=false;
var pressingLeft:布尔值=false;
变量comp:Boolean=false;
无功转速:int=10;
var-char:字符;
var-pu:通电;
var hit:hit1
结束:游戏结束;
公共功能Main()
{
bg=新背景;
addChild(bg);
char=新字符();
addChild(char);
char.x=stage.stageWidth/2;
字符y=488;
b=新气泡();
儿童(b);
b2=新气泡();
addChild(b2);
b2.y=+b2.2高度;
s=新海藻();
addChild(s);
pu=新加电();
阿迪奇尔德(pu);
pu.x=200;
pu.y=450;
pu.高度=50;
pu.scaleX=pu.scaleY;
pu.gotoAndStop(“旋转”);
hit=新hit1;
addChild(hit);
hit.x=char.x
hit.y=char.y-50
结束=新的结局();
end.x=stage.stageWidth/2;
end.y=stage.stageHeight/2;
stage.addEventListener(Event.ENTER_FRAME,onFrame);
stage.addEventListener(KeyboardEvent.KEY_向下,按键);
stage.addEventListener(KeyboardEvent.KEY\u UP,keyReleased);
}//main()
框架上的公共功能(e:事件)
{
如果(!comp)
//气泡运动
b、 y-=1;
b2.y-=1;
如果(b.y>=舞台高度)
{
b、 y=b2.y+bg.height;
}
else if(b2.y>=舞台高度)
{
b2.y=b.y+b2.H;
}
//背景与人物运动
如果(按右键和字符xstage.stageWidth)
{
bg.x-=速度*0.4;
s、 x-=速度*0.6;
pu.x-=速度*0.4;
}
否则如果(按Right==true)
{
字符x+=速度*0.4;
}
如果(按左==true&&char.x>stage.stageWidth/2)
{
字符x-=速度*0.4;
}
else if(按left==true&&bg.x stage.stageWidth)
{
char.x=stage.stageWidth;
}
else if(字符x<0)
{
字符x=0;
}
//字符外观方向
如果(按左==真)
{
char.scaleX=-1;
hit.x=char.x
}
如果(按右键==真)
{
char.scaleX=1;
hit.x=char.x
}
//人物动作
如果(按右键| |按左键)
{
字符:gotoAndStop(“步行”);
}
否则,如果(!按右| |!按左)
{
字符gotoAndStop(“空闲”);
}
//接通电源
if(pu.hitTestObject(hit))
{
字符gotoAndStop(“电源”);
comp=真;
pu.gotoAndStop(“消失”);
}
//!
else if(comp)//如果完成为true
{
var animation=char.getChildAt(2)//
if(animation.currentFrame==animation.totalFrames)
{
动画。停止();
addChild(完),;
}
}//公司
}//镜框
按下公共功能键(k:键盘事件)
{
if(k.keyCode==Keyboard.RIGHT)
{
按right=true;
}
else if(k.keyCode==Keyboard.LEFT)