高级对象与角色控制技术解析
立即解锁
发布时间: 2025-08-25 00:05:52 阅读量: 1 订阅数: 6 

# 高级对象与角色控制技术解析
## 1. 全方位发射弹丸案例分析
在这个案例中,我们可以通过拖动鼠标让 Button Fairy 在舞台上飞行。同时,其“魔杖”会围绕精灵旋转并指向鼠标光标,点击鼠标左键就能向各个方向发射星星,星星碰到舞台边界会被移除。这个案例展示了以下关键游戏设计技术:
- 如何让一个对象围绕另一个对象旋转
- 如何全方位发射弹丸
- 如何使用组合技术,通过一个 `GameObject` 类创建所有游戏对象
### 1.1 代码实现
以下是实现该功能的完整应用类代码:
```actionscript
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
[SWF(width="550", height="400",
backgroundColor="#FFFFFF", frameRate="60")]
public class ButtonFairy extends Sprite
{
//Embed the images
[Embed(source="../images/fairy.png")]
private var FairyImage:Class;
[Embed(source="../images/wand.png")]
private var WandImage:Class;
[Embed(source="../images/star.png")]
private var StarImage:Class;
//Private properties
private const EASING:Number = 0.1;
private var _fairyImage:DisplayObject = new FairyImage();
private var _wandImage:DisplayObject = new WandImage();
//Create the fairy and wand game objects
//(The images will be centered by the GameObject class)
private var _fairy:GameObject = new GameObject(_fairyImage);
private var _wand:GameObject = new GameObject(_wandImage);
//A variable to store the angle between
//the mouse and the center of the fairy
private var _angle:Number;
//An array to store the stars
private var _stars:Array = new Array();
public function ButtonFairy()
{
//Add the fairy and the wand to the stage
stage.addChild(_fairy);
stage.addChild(_wand);
//Hide the mouse
Mouse.hide();
//Add the event listeners
stage.addEventListener
(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener
(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private function enterFrameHandler(event:Event):void
{
//Find the angle between the center of the
//fairy and the mouse
_angle
= Math.atan2
(
_fairy.y - stage.mouseY,
_fairy.x - stage.mouseX
);
//Move the wand around the fairy
var radius:int = -50;
_wand.x = _fairy.x + (radius * Math.cos(_angle));
_wand.y = _fairy.y + (radius * Math.sin(_angle));
//Figure out the distance between the
//mouse and the center of the fairy
var vx:Number = stage.mouseX - _fairy.x;
var vy:Number = stage.mouseY - _fairy.y;
var distance:Number = Math.sqrt(vx * vx + vy * vy);
//Move the fairy if it's more than 1 pixel away from the mouse
if (distance >= 1)
{
_fairy.x += vx * EASING;
_fairy.y += vy * EASING;
}
//Move the stars
for(var i:int = 0; i < _stars.length; i++)
{
var star:GameObject = _stars[i];
star.x += star.vx;
star.y += star.vy;
//check the star's stage boundaries
if (star.y < 0
|| star.x < 0
|| star.x > stage.stageWidth
|| star.y > stage.stageHeight)
{
//Remove the star from the stage
stage.removeChild(star);
star = null;
//Remove the star from the _stars array
_stars.splice(i,1);
//Reduce the loop counter
//by one to compensate
//for the removed star
i--;
}
}
}
private function mouseDownHandler(event:MouseEvent):void
{
//Create a star and add it to the stage
var starImage:DisplayObject = new StarImage();
var star:Game
```
0
0
复制全文
相关推荐








