加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【AS3】小船在窗口来回运动

发布时间:2020-12-15 06:48:57 所属栏目:百科 来源:网络整理
导读:?有时间看看AS了,今天看书做了一个很简单的东西。 是一个小船,可以来回游走,当它走到边界,便掉头走,一直反复.... var speed:int=10;boat.addEventListener(Event.ENTER_FRAME,boatMove);function boatMove(evt:Event):void {if (stage.stageWidth=evt.t

?有时间看看AS了,今天看书做了一个很简单的东西。

是一个小船,可以来回游走,当它走到边界,便掉头走,一直反复....

var speed:int=10;
boat.addEventListener(Event.ENTER_FRAME,boatMove);
function boatMove(evt:Event):void {

	if (stage.stageWidth<=evt.target.x && evt.target.x<stage.stageWidth+10) {
		speed *=-1;
		evt.target.nextFrame();
	} else if (0<=evt.target.x && evt.target.x<10) {
		speed *=-1;
		evt.target.prevFrame();
	}
	
	evt.target.x +=speed;
	trace(evt.target.x,speed);
}


?

代码说明:小船作为元件,取名为 "boat",对它添加监听,每次载入一帧,就执行boatMove的代码。

?

几点注意事项:

1.上例中的速度为10,可是当小船到达边界时,不一定正好 小船的X坐标到达舞台的边界,即 evt.target.x=stage.stageWidth 。所以小船什么时候掉头呢?应该给它10px的可能空间。写成evt.target.x=stage.stageWidth ,小船可能永远都无法掉头。

2.stage.stageWidth???? vs??? stage.width:我在FLASH的输出窗口测试了一下,把以上代码的最后一句改为了 trace(evt.target.x,stage.stageWidth,stage.width);比较一下二者的差别。

我发现:stage.stageWidth?属性跟随窗口的大小变化而变化,stage.width属性跟随运动物体的到达方位变化而变化。前者是窗口的实际大小,可以在运行.swf文件时随意改变大小,但改变了大小会对程序有很大影响,甚至产生错误。后者是整个舞台的大小,包含了所有的物体。

帮助文件中的解释:
Stage.width

public static width :? Number

Property (read-only); indicates the current width,in pixels,of the Stage. When the value of Stage.scaleMode is "noScale",the width property represents the width of Flash Player. This means that Stage.width will vary as you resize the player window. When the value of Stage.scaleMode is not "noScale",width represents the width of the SWF file as set at author-time in the Document Properties dialog box. This means that the value of width will stay constant as you resize the player window.

3.bug:当改变窗口大小后,如果evt.target.x>>stage.stageWidth,船将永远找不到边界。

问题:如何锁定窗口大小呢?

stage.scaleMode = StageScaleMode.NO_SCALE;???? --不理解.

4.元件boat中其实有两帧,第一帧是右走时船的样子;第二帧是左走时。为了不让boat一直旋转方向,在boat中打入代码:stop();

以下两句便容易理解:

跳到下一帧: evt.target.nextFrame();

跳到上一帧:evt.target.prevFrame();

总结:程序很简单,但里面还是涉及到很多的问题,另外,图片素材来自参考文献。

参考文献:

【1】叶翊霳《FLASH actionscript 程序设计经典商业范例集》清华大学出版社 1-11

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读