佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1824|回复: 8

简易AS教学:控制MC移动篇

[复制链接]
发表于 21-5-2007 07:09 PM | 显示全部楼层 |阅读模式
小弟初来乍到,发现这里的FLASH ACTION SCRIPT教学贴匮乏,
所以便心血来潮开了这个贴来教FLASH AS新手一些简易的技巧~

小弟的兴趣是制作FLASH GAME,所以这篇教学会传授一些简单的写GAME技巧,
希望通过此贴能够激发大家制作FLASH GAME的兴趣~
如果此贴反应好的话,小弟还会陆续开多几贴将小弟用来制作FLASH GAME的简易技巧传授给大家。


简易AS教学:控制MC移动篇

什么是MC?MC就是FLASH里的Movie Clip。
通过AS,我们可以控制某个MC执行的任务。

1.打开FLASH,在画板里居中绘制一个圆圈。

2.点选该圆圈,按F8将之convert成Movie Clip(MC),Name可以任取。

3.点选该MC(即你画的圆圈),按F9打开ACTION面板,先关闭右上角的Script Assist,然后在面板里输入以下程式:

onClipEvent (enterFrame) {
        if (Key.isDown(Key.LEFT)) {
                this._x -= 10;
        }
        if (Key.isDown(Key.RIGHT)) {
                this._x += 10;
        }
        if (Key.isDown(Key.UP)) {
                this._y -= 10;
        }
        if (Key.isDown(Key.DOWN)) {
                this._y += 10;
        }
}


4.完毕。按<CTRL>+<ENTER>测试影片,用方向键控制圆圈上下左右移动,双键齐下还能斜移呢。


AS解说:

1.if (Key.isDown(Key.XXXX))

就是说当你按下某键的时候,该执行什么任务。

2.this._x -= 10; (可解成 this._x = this._x - 10)

this是指该MC,_x和_y是指该MC的position。还记得中学时数学课学的xy轴座标吗?
_x是该MC的平行位置,_y则是垂直位置,
_x向右递增,向左递减;
_y则向下递增,向上递减,
若你的Document Size是以pixels来计算的话,this._x -= 10则是指该MC向左移动10pixels。

下载范例:
http://wtfteam.brinkster.net/swf/learn/mcmove.swf


对小弟制作的FLASH GAME有兴趣的话可以来此贴看看:
http://chinese.cari.com.my/myforum/viewthread.php?tid=873142&extra=page%3D1

或官网:
http://wtfteam.brinkster.net/littlemonk/

[ 本帖最后由 weekie 于 21-5-2007 07:13 PM 编辑 ]

评分

参与人数 1积分 +50 收起 理由
天魔神 + 50 原创内容

查看全部评分

回复

使用道具 举报


ADVERTISEMENT

发表于 1-7-2007 04:25 PM | 显示全部楼层
Press arrow key to move the circle?

好像动不了~
回复

使用道具 举报

 楼主| 发表于 4-7-2007 12:13 PM | 显示全部楼层
原帖由 shinn06 于 1-7-2007 04:25 PM 发表
Press arrow key to move the circle?

好像动不了~


可以啊~
你用网页浏览器如Firefox操作时,
必须先click该页面的content后才能用键盘操作~
回复

使用道具 举报

发表于 4-7-2007 04:35 PM | 显示全部楼层
是一个好的简单教学。已经报告,加分加精。
加油!
回复

使用道具 举报

发表于 5-7-2007 10:58 PM | 显示全部楼层
原帖由 weekie 于 21-5-2007 07:09 PM 发表
onClipEvent (enterFrame) {
        if (Key.isDown(Key.LEFT)) {
                this._x -= 10;
        }
        if (Key.isDown(Key.RIGHT)) {
                this._x += 10;
        }
        if (Key.isDown(Key.UP)) {
                this._y -= 10;
        }
        if (Key.isDown(Key.DOWN)) {
                this._y += 10;
        }
}


請問如果我不要以LEFT RIGHT UP DOWN key 來控制我要以 A D W S key 來控制。。那麼我該怎麼寫呢?
回复

使用道具 举报

 楼主| 发表于 6-7-2007 01:14 AM | 显示全部楼层
原帖由 大老权 于 5-7-2007 10:58 PM 发表


請問如果我不要以LEFT RIGHT UP DOWN key 來控制我要以 A D W S key 來控制。。那麼我該怎麼寫呢?


改成以下那样就行了~

onClipEvent (enterFrame) {
        if (Key.isDown(65)) {
                this._x -= 10;
        }
        if (Key.isDown(68)) {
                this._x += 10;
        }
        if (Key.isDown(87)) {
                this._y -= 10;
        }
        if (Key.isDown(83)) {
                this._y += 10;
        }
}


上面代码所看到的数字(65/68/87/83)其实就是AS里的Key Code,是用来辨认键盘的键的。

原码应该是Key.isDown(code:Number)
因为"A"的Key Code是"65",所以该程式就写成Key.isDown(65)

想知道其它键的Key Code是什么,可以按F1去Flash Help里输入Keyboard Keys and Key Code Values搜索。


补充:
其实也可以用以下代码施行移动MC的功能:

on (keyPress "a" ) {
    this._x -= 10;
}
on (keyPress "d" ) {
    this._x += 10;
}
on (keyPress "w" ) {
    this._y -= 10;
}
on (keyPress "s" ) {
    this._y += 10;
}


不过这样的写法缺乏灵活性,明显的例子是不能双键齐下实现斜移。

[ 本帖最后由 weekie 于 6-7-2007 01:29 AM 编辑 ]
回复

使用道具 举报

Follow Us
发表于 6-7-2007 02:05 PM | 显示全部楼层

回复 #6 weekie 的帖子

謝謝。。我還要問。。
為什麼我的HELP什麼也沒有呢?
回复

使用道具 举报

 楼主| 发表于 7-7-2007 09:19 PM | 显示全部楼层
原帖由 大老权 于 6-7-2007 02:05 PM 发表
謝謝。。我還要問。。
為什麼我的HELP什麼也沒有呢?


嗯。。。如果没有的话,也可以试下用这个方法检测你要的key code~

在你的MC里放入以下程式:

onClipEvent(keyDown) {
    trace(Key.getCode());
}


先按CTRL+ENTER测试影片,
然后在按键盘的键,会跳出一个Output栏,显示的数字就是你按的键的Key Code。

补充:
你在该程式看到的trace(),通常是用来检测variables等等的。

简单例子:
在你的MC里放入以下程式:

onClipEvent (enterFrame) {
        if (Key.isDown(Key.LEFT)) {
                this._x -= 10;
                trace("_x = "+this._x);
        }
        if (Key.isDown(Key.RIGHT)) {
                this._x += 10;
                trace("_x = "+this._x);
        }
        if (Key.isDown(Key.UP)) {
                this._y -= 10;
                trace("_y = "+this._y);
        }
        if (Key.isDown(Key.DOWN)) {
                this._y += 10;
                trace("_y = "+this._y);
        }
}


用方向键移动MC,在Output栏显示的就是MC目前的_x和_y值。
回复

使用道具 举报


ADVERTISEMENT

发表于 8-7-2007 01:04 PM | 显示全部楼层
原帖由 大老权 于 6-7-2007 02:05 PM 发表
謝謝。。我還要問。。
為什麼我的HELP什麼也沒有呢?



怎麼可能會沒有, 所有用法都有附帶例子, 是你沒用心去看吧

isDown (Key.isDown method)public static isDown(code:Number) : Boolean

Returns true if the key specified in keycode is pressed; false otherwise. On the Macintosh, the key code values for the Caps Lock and Num Lock keys are identical.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
Availability: ActionScript 1.0; Flash Player 5
Parameterscode:Number - The key code value assigned to a specific key or a Key class property associated with a specific key.
ReturnsBoolean - The value true if the key specified in keycode is pressed; false otherwise.
ExampleThe following script lets the user control a movie clip's (car_mc) location:
car_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
this._x += 10;
} else if (Key.isDown(Key.LEFT)) {
this._x -= 10;
}
};
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 12-9-2025 05:10 AM , Processed in 0.165925 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表