flash as3.0 物体运动的代码怎么写?
控制一个物体运动,除了用“mc.x+=1”这个方法,还有没有别的方法控制,运动的速度,加速度,方向,应该用哪个类?有没有一个方法使元件沿一定角度运动,也就是用角度控制方向...
控制一个物体运动,除了用“mc.x+=1”这个方法,还有没有别的方法控制,运动的速度,加速度,方向,应该用哪个类?
有没有一个方法使元件沿一定角度运动,也就是用角度控制方向,用一个值控制速度? 展开
有没有一个方法使元件沿一定角度运动,也就是用角度控制方向,用一个值控制速度? 展开
3个回答
展开全部
新建一个FLA文件,把下列代码粘贴至第一帧的代码栏里,ctrl+enter看效果
var ball:Sprite;
var angle:Number = 0;
var speed:Number = 0;
var tf_angle:TextField;
var tf_speed:TextField;
var lab_angle:TextField;
var lab_speed:TextField;
var bt_changeArgs:SimpleButton;
var bt_reset:SimpleButton;
drawBall();
drawUI();
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function drawBall():void {
ball = new Sprite();
with (ball.graphics) {
beginFill(0x000000);
drawCircle(0,0,10);
endFill();
}
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
this.addChild(ball);
}
function drawUI():void {
lab_angle = new TextField();
lab_angle.x = 10;
lab_angle.y = 10;
lab_angle.text = "角度";
lab_angle.selectable = false;
lab_angle.width = 40;
lab_angle.height = 20;
this.addChild(lab_angle);
tf_angle = new TextField();
tf_angle.x = 50;
tf_angle.y = 10;
tf_angle.border = true;
tf_angle.width = 50;
tf_angle.height = 20;
tf_angle.type = TextFieldType.INPUT;
tf_angle.text = "0";
this.addChild(tf_angle);
lab_speed = new TextField();
lab_speed.x = 10;
lab_speed.y = 50;
lab_speed.text = "速度";
lab_speed.selectable = false;
lab_speed.width = 40;
lab_speed.height = 20;
this.addChild(lab_speed);
tf_speed = new TextField();
tf_speed.x = 50;
tf_speed.y = 50;
tf_speed.border = true;
tf_speed.width = 50;
tf_speed.height = 20;
tf_speed.type = TextFieldType.INPUT;
tf_speed.text = "0";
this.addChild(tf_speed);
bt_changeArgs = new SimpleButton(getSprite(1,1),getSprite(1,2),getSprite(1,3),getSprite(1,4));
bt_changeArgs.x = 50;
bt_changeArgs.y = 90;
bt_changeArgs.addEventListener(MouseEvent.CLICK,changeArgs);
this.addChild(bt_changeArgs);
bt_reset = new SimpleButton(getSprite(2,1),getSprite(2,2),getSprite(2,3),getSprite(2,4));
bt_reset.x = 50;
bt_reset.y = 130;
bt_reset.addEventListener(MouseEvent.CLICK,resetBall);
this.addChild(bt_reset);
}
function changeArgs(e:MouseEvent):void {
angle = Number(tf_angle.text);
speed = Number(tf_speed.text);
}
function resetBall(e:MouseEvent):void{
angle = Number(tf_angle.text);
speed = Number(tf_speed.text);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
}
function enterFrameHandler(e:Event):void {
ball.x += speed * Math.cos(angle * Math.PI / 180);
ball.y+=speed*Math.sin(angle*Math.PI/180);
}
function getSprite(type:int,state:int):Sprite {
var sprite:Sprite = new Sprite();
var lab:TextField = new TextField();
switch(type){
case 1:
lab.text="点击这里改变参数";
break;
case 2:
lab.text="点击这里复原小球";
break;
}
switch (state) {
case 1 :
sprite.graphics.beginFill(0x666666);
lab.textColor=0xFFFFFF;
break;
case 2 :
sprite.graphics.beginFill(0xCCCCCC);
lab.textColor=0xFFFFFF;
break;
case 3 :
sprite.graphics.beginFill(0xFFFFFF);
lab.textColor=0x000000;
break;
case 4 :
sprite.graphics.beginFill(0x666666);
break;
}
sprite.graphics.drawRect(0,0,100,20);
sprite.addChild(lab);
return sprite;
}
var ball:Sprite;
var angle:Number = 0;
var speed:Number = 0;
var tf_angle:TextField;
var tf_speed:TextField;
var lab_angle:TextField;
var lab_speed:TextField;
var bt_changeArgs:SimpleButton;
var bt_reset:SimpleButton;
drawBall();
drawUI();
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function drawBall():void {
ball = new Sprite();
with (ball.graphics) {
beginFill(0x000000);
drawCircle(0,0,10);
endFill();
}
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
this.addChild(ball);
}
function drawUI():void {
lab_angle = new TextField();
lab_angle.x = 10;
lab_angle.y = 10;
lab_angle.text = "角度";
lab_angle.selectable = false;
lab_angle.width = 40;
lab_angle.height = 20;
this.addChild(lab_angle);
tf_angle = new TextField();
tf_angle.x = 50;
tf_angle.y = 10;
tf_angle.border = true;
tf_angle.width = 50;
tf_angle.height = 20;
tf_angle.type = TextFieldType.INPUT;
tf_angle.text = "0";
this.addChild(tf_angle);
lab_speed = new TextField();
lab_speed.x = 10;
lab_speed.y = 50;
lab_speed.text = "速度";
lab_speed.selectable = false;
lab_speed.width = 40;
lab_speed.height = 20;
this.addChild(lab_speed);
tf_speed = new TextField();
tf_speed.x = 50;
tf_speed.y = 50;
tf_speed.border = true;
tf_speed.width = 50;
tf_speed.height = 20;
tf_speed.type = TextFieldType.INPUT;
tf_speed.text = "0";
this.addChild(tf_speed);
bt_changeArgs = new SimpleButton(getSprite(1,1),getSprite(1,2),getSprite(1,3),getSprite(1,4));
bt_changeArgs.x = 50;
bt_changeArgs.y = 90;
bt_changeArgs.addEventListener(MouseEvent.CLICK,changeArgs);
this.addChild(bt_changeArgs);
bt_reset = new SimpleButton(getSprite(2,1),getSprite(2,2),getSprite(2,3),getSprite(2,4));
bt_reset.x = 50;
bt_reset.y = 130;
bt_reset.addEventListener(MouseEvent.CLICK,resetBall);
this.addChild(bt_reset);
}
function changeArgs(e:MouseEvent):void {
angle = Number(tf_angle.text);
speed = Number(tf_speed.text);
}
function resetBall(e:MouseEvent):void{
angle = Number(tf_angle.text);
speed = Number(tf_speed.text);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
}
function enterFrameHandler(e:Event):void {
ball.x += speed * Math.cos(angle * Math.PI / 180);
ball.y+=speed*Math.sin(angle*Math.PI/180);
}
function getSprite(type:int,state:int):Sprite {
var sprite:Sprite = new Sprite();
var lab:TextField = new TextField();
switch(type){
case 1:
lab.text="点击这里改变参数";
break;
case 2:
lab.text="点击这里复原小球";
break;
}
switch (state) {
case 1 :
sprite.graphics.beginFill(0x666666);
lab.textColor=0xFFFFFF;
break;
case 2 :
sprite.graphics.beginFill(0xCCCCCC);
lab.textColor=0xFFFFFF;
break;
case 3 :
sprite.graphics.beginFill(0xFFFFFF);
lab.textColor=0x000000;
break;
case 4 :
sprite.graphics.beginFill(0x666666);
break;
}
sprite.graphics.drawRect(0,0,100,20);
sprite.addChild(lab);
return sprite;
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
s速度控制试试Tween类,至于移动除了直接控制坐标,目前我还不懂更好的方法。你可以用
var max:Matrix=new Matrix();
max.translate(x,y);
zz.transform.matrix=max;
矩阵来控制,但是这样变得更麻烦了啊..........
var max:Matrix=new Matrix();
max.translate(x,y);
zz.transform.matrix=max;
矩阵来控制,但是这样变得更麻烦了啊..........
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var my_time = setInterval(this, "gogogo", 20);
var k = mc.x+200;
function gogogo() {
mc.x -= (mc.x-k)/10;
}
//试下这个,嘿嘿
var k = mc.x+200;
function gogogo() {
mc.x -= (mc.x-k)/10;
}
//试下这个,嘿嘿
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询