MTM动画教程学习笔记1【第七章 用户交互:移动物体】

本博客详细介绍了如何在舞台环境中实现物体的拖动、放开及抛掷操作,包括物体运动状态的更新、碰撞检测与反弹效果。通过实例代码展示了如何使用Flash平台的事件处理与物理模拟来创建互动性强的游戏元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第七章 用户交互 :移动物体


1.拖动和放开物体
startDrag(lockCenter,rect)
lockCenter 表示按下后鼠标时候要和物体的中心点对齐,rect表示锁定拖拽的范围。
stopDrag();
与鼠标的MOUSE_DOWN,MOUSE_UP联合使用


2.抛。

import flash.events.MouseEvent;
import flash.display.*;
import flash.events.Event;

var vx:Number;
var vy:Number;
var bounce:Number = -0.7;
var gravity:Number = 0.5;
var oldX:Number;
var oldY:Number;

init();

function init():void
{
	stage.align = StageAlign.TOP_LEFT;
	stage.scaleMode = StageScaleMode.NO_SCALE;
	
	circle.x =stage.stageWidth / 2;
	circle.y = stage.stageHeight/2;
	vx = Math.random() * 10 - 5;
	vy = -10;
	circle.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
	addEventListener(Event.ENTER_FRAME,onFrame);
}

function onFrame(e:Event):void
{
	circle.x += vx;
	vy += gravity;
	circle.y += vy;
	
	if(circle.x + circle.width / 2 > stage.stageWidth)
	{
		circle.x = stage.stageWidth - circle.width / 2;
		vx *= bounce;
	}
	else if(circle.x - circle.width / 2 < 0)
	{
		circle.x = circle.width / 2;
		vx *= bounce;
	}
	if(circle.y + circle.height / 2 > stage.stageHeight)
	{
		circle.y = stage.stageHeight - circle.height/2;
		vy *= bounce;
	}
	else if(circle.y - circle.height / 2 < 0)
	{
		circle.y = circle.height / 2;
		vy *= bounce;
	}
}

function onDown(e:MouseEvent):void
{
	oldX = circle.x;
	oldY = circle.y;
	stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
	(circle as Sprite).startDrag();  // 第一个参数 lockCenter表示鼠标是否与拖动对象中心点对齐
	removeEventListener(Event.ENTER_FRAME,onFrame);
	addEventListener(Event.ENTER_FRAME,trackVectory);
}

function onUp(e:MouseEvent):void
{
	stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
	(circle as Sprite).stopDrag();
	removeEventListener(Event.ENTER_FRAME,trackVectory);
	addEventListener(Event.ENTER_FRAME,onFrame);
}

function trackVectory(e:Event):void
{
	vx = circle.x - oldX;
	vy = circle.y - oldY;
	oldX = circle.x;
	oldY = circle.y;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值