上下左右光标控制物体跳动
上下左右光标控制物体跳动
//物体左右跳跃的类as2.0
//水平速度
var xsd:Number = 10;
//重力
var numg:Number = 5;
//垂直速度
var ysd:Number = -18;
//声明对象
//对象名称obj_实例名称,
var obj_mc = new jump (mc, xsd, numg, ysd);
_root.onEnterFrame = function ()
{
obj_mc.onEnterFrame ();
};
var keyListener:Object = new Object ();
keyListener.onKeyUp = function ()
{
switch (Key.getCode ()) {
case 37 :
//trace ("<");
obj_mc.go(-1);
break;
case 39 :
//trace (">");
obj_mc.go(1);
break;
}
};
Key.addListener (keyListener);
//物体左右跳跃的类as2.0
//水平速度
var xsd:Number = 10;
//重力
var numg:Number = 5;
//垂直速度
var ysd:Number = -18;
//声明对象
//对象名称obj_实例名称,
var obj_mc = new jump (mc, xsd, numg, ysd);
_root.onEnterFrame = function ()
{
obj_mc.onEnterFrame ();
};
var keyListener:Object = new Object ();
keyListener.onKeyUp = function ()
{
switch (Key.getCode ()) {
case 37 :
//trace ("<");
obj_mc.go(-1);
break;
case 39 :
//trace (">");
obj_mc.go(1);
break;
}
};
Key.addListener (keyListener);
类文件:##########################################
class jump
{
//mc目标影片
var mc:MovieClip;
//水平速度
var xsd:Number;
//重力
var numg:Number;
//垂直速度
var ysd:Number;
var iniYsd:Number;
//地面的位置
var iniY:Number;
private var jumpx:Number;
//是不是第一次开始跳跃
private var isFirst:Boolean = true;
//是否开始跳
private var isJump:Boolean = false;
//初使化
function jump (mc:MovieClip, xsd:Number, numg:Number, ysd:Number)
{
this.mc = mc;
this.xsd = xsd;
this.numg = numg;
//
this.iniYsd = ysd;
this.ysd = ysd;
//
iniY = mc._y;
}
//跳
function go (jumpx:Number)
{
if (!isJump) {
isJump = true;
this.jumpx = jumpx;
}
}
// 检测是否跳,并作跳的处理
function jumping ()
{
if (!isJump) {
return 0;
}
//
if (isFirst) {
ysd += numg;
mc._y += ysd;
mc._x += xsd * jumpx;
isFirst = false;
}
else {
if ((mc._y + ysd + numg) < iniY) {
ysd += numg;
mc._y += ysd;
mc._x += xsd * jumpx;
}
else {
mc._y = iniY;
isFirst = true;
isJump = false;
this.ysd = this.iniYsd;
}
}
}
//要循环都统一放在这里。
function onEnterFrame ()
{
jumping ();
}
}
{
//mc目标影片
var mc:MovieClip;
//水平速度
var xsd:Number;
//重力
var numg:Number;
//垂直速度
var ysd:Number;
var iniYsd:Number;
//地面的位置
var iniY:Number;
private var jumpx:Number;
//是不是第一次开始跳跃
private var isFirst:Boolean = true;
//是否开始跳
private var isJump:Boolean = false;
//初使化
function jump (mc:MovieClip, xsd:Number, numg:Number, ysd:Number)
{
this.mc = mc;
this.xsd = xsd;
this.numg = numg;
//
this.iniYsd = ysd;
this.ysd = ysd;
//
iniY = mc._y;
}
//跳
function go (jumpx:Number)
{
if (!isJump) {
isJump = true;
this.jumpx = jumpx;
}
}
// 检测是否跳,并作跳的处理
function jumping ()
{
if (!isJump) {
return 0;
}
//
if (isFirst) {
ysd += numg;
mc._y += ysd;
mc._x += xsd * jumpx;
isFirst = false;
}
else {
if ((mc._y + ysd + numg) < iniY) {
ysd += numg;
mc._y += ysd;
mc._x += xsd * jumpx;
}
else {
mc._y = iniY;
isFirst = true;
isJump = false;
this.ysd = this.iniYsd;
}
}
}
//要循环都统一放在这里。
function onEnterFrame ()
{
jumping ();
}
}
补充:flash教程,动画技术