11/9 ActionScript反転・ボタンで制御
ボタンを押されるたびに10px移動する。
オブジェクトアクション
- 各ボタンに記述する。
- ボタンのインスタンスはそれぞれに記述するので、名前は一緒で良い。
//上ボタン on (release) { this.panda_ms._y-= 10; } //下ボタン on (release) { this.panda_ms._y+= 10; } //右ボタン on (release) { this.panda_ms._x-= 10; } //左ボタン on (release) { this.panda_ms._X+= 10; }
- タイムラインに記述する。
- ボタンのインスタンス名をそれぞれのボタンに指定する。
var rate:Number=20; _root.top_btn.onRelease=function() { _root.panda_mc._y-=rate; if(panda_mc._y<=0){ panda_mc._y=450; } } _root.right_btn.onRelease=function() { _root.panda_mc._x+=rate; if(panda_mc._x>=550){ panda_mc._x=-70; } } _root.bottom_btn.onRelease=function() { _root.panda_mc._y+=rate; if(panda_mc._y>=400){ panda_mc._y=-50; } } _root.left_btn.onRelease=function() { _root.panda_mc._x-=rate; if(panda_mc._x<=0){ panda_mc._x=600; } }
ボタンを押し続けると10px移動して、離すと止まる。
- 各ボタンに設定する場合
on (press):ボタンを押したとき
onEnterFrame:読み込まれるたびに
undefined:値が設定されていないことを表す。マウスを離したときの設定に使う。
//上ボタン on (press) { panda_mc.onEnterFrame = function() { panda_mc._y -= 10; if(panda_mc._y <= -10) { panda_mc._y=450; } } } on (release) { panda_mc.onEnterFrame = undefined; } //下ボタン on (press) { panda_mc.onEnterFrame = function() { panda_mc._y += 10; if(panda_mc._y >= 400) { panda_mc._y= -50; } } } on(release) { panda_mc.onEnterFrame = undefined; } //右ボタン on (press) { panda_mc.onEnterFrame = function() { panda_mc._x -= 10; if(panda_mc._x <= -10) { panda_mc._x= 600; } } } on (release) { panda_mc.onEnterFrame = undefined; } //左ボタン on (press) { panda_mc.onEnterFrame = function() { panda_mc._x += 10; if(panda_mc._x >= 550) { panda_mc._x= -70; } } } on(release) { panda_mc.onEnterFrame = undefined; }
左右の向きを反転
- x座標をマイナス値にする。 _xscale *= -1;
onClipEvent (load) { var speedX:Number=5;//X移動の速度 var speedY:Number=5;//Y移動の速度 var rightEdge:Number=550;//右端の座標 var leftEdge:Number=0;//左端の座標 var topEdge:Number=0;//上の座標 var bottomEdge:Number=400;//下の座標 } onClipEvent (enterFrame) { if(_x >rightEdge-_width/2|| _x< leftEdge + _width/2) { speedX*=-1; _xscale*=-1; } if(_y >bottomEdge -_height/2||_y< topEdge + _height/2) { speedY*=-1; } _x +=speedX; _y +=speedY; }