我的对象是通过鼠标移动控制的(暂时)......由于它的控制方式(对象总是远离光标),我想添加约束,这样它不仅可以保持在舞台内,而且还可以在运动边界和舞台边缘......
因此,如果物体移动得太靠近舞台限制,光标将有空间将其移回播放区域。
目前,我在舞台上动态创建了一个矩形,我想我可以将移动限制在这个矩形区域内,这样可以在边缘留下足够的空间......我该怎么做?
然而,如果有更好/更容易的方法来获得理想的结果......我全都耳朵。
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Shape;
var rectangle:Shape = new Shape;
//initialize "rectangle" shape
rectangle.graphics.beginFill(0xCCCCCC);
//choose colour for fill - black
rectangle.graphics.drawRect(50, 50, 450, 300);
//draw shape
rectangle.graphics.endFill();
//end fill
addChild(rectangle);
//add "rectangle" to stage
var hero:MovieClip = new hero_mc();
//initialize "hero" object - "hero_mc"
hero.x = stage.stageWidth / 2;
hero.y = stage.stageHeight / 2;
//set spawn location, centre stage
addChild(hero);
//add "hero" to stage
function cursorHold(evt:Event):void {
trace("moving");
var dX:Number = hero.x - stage.mouseX;
//get adjacent
var dY:Number = hero.y - stage.mouseY;
//get opposite
var r:Number = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
//get hypotenuse
var angle:Number = Math.acos(dX/r)*180/Math.PI;
//get angle
var radians:Number = deg2rad(angle);
//call conversion function for angle
var speed:Number = 1.5;
//set speed
var xV:Number = Math.cos(radians) * speed;
//get x velocity
var yV:Number = Math.sin(radians) * speed;
//get y velocity
hero.x += xV;
//move hero along new x velocity
if (stage.mouseY > hero.y) {
hero.y -= yV;
} else {
hero.y += yV;
}
//move hero along new y velocity
}
function deg2rad(deg:Number):Number {
return deg * (Math.PI / 180);
//convert degrees to radians
}
stage.addEventListener(Event.ENTER_FRAME, cursorHold, false, 0, true);
这是一张图片:https://lh5.googleusercontent.com/lWG8uR9MLK32oHXX26JBiLtBdvmiICFuxOQakQESnBY=w552-h402-no
所以,“hero_mc”不应该移动到白色区域,但它仍然应该移动(用鼠标移动刮擦边缘。
道歉,如果代码是一团糟...我对ActionScript 3.0很新。任何有关清理它的提示也非常受欢迎。
答案 0 :(得分:0)
这里有一些提示给你..
首先,我会为边距添加一个var来动态绘制舞台的活动区域,然后使用它创建矩形,就像这样 -
// margin of stage
var margin:uint = 20;
rectangle.graphics.drawRect(margin, margin, stage.stageWidth-(2*margin), stage.stageHeight-(2*margin));
然后我还会添加一个var来获取英雄mc的中心 -
var heroCenter:Number = hero.width * .5;
最后是一个var,用于将yV设置为+ =或 - =并使用if来设置它 -
var posNeg:Number = 0;
if(stage.mouseY > hero.y){
posNeg = -1;
} else {
posNeg = 1;
}
hero.y += yV*posNeg;
这是完整的代码。我把它设置为10来加快测试速度。
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Shape;
// margin of stage
var margin:uint = 20;
var rectangle:Shape = new Shape;
//initialize "rectangle" shape
rectangle.graphics.beginFill(0xCCCCCC);
//choose colour for fill - black
rectangle.graphics.drawRect(margin, margin, stage.stageWidth-(2*margin), stage.stageHeight-(2*margin));
//draw shape
rectangle.graphics.endFill();
//end fill
addChild(rectangle);
//add "rectangle" to stage
var hero:MovieClip = new hero_mc();
//initialize "hero" object - "hero_mc"
// half hero to make sure hero goes completely to edge of stage without going over or under
// best if the hero widht is an even number.
var heroCenter:Number = hero.width * .5;
hero.x = stage.stageWidth / 2;
hero.y = stage.stageHeight / 2;
//set spawn location, centre stage
addChild(hero);
//add "hero" to stage
var posNeg:Number = 0;
function cursorHold(evt:Event):void {
//trace("moving");
var dX:Number = hero.x - stage.mouseX;
//get adjacent
var dY:Number = hero.y - stage.mouseY;
//get opposite
var r:Number = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
//get hypotenuse
var angle:Number = Math.acos(dX/r)*180/Math.PI;
//get angle
var radians:Number = deg2rad(angle);
//call conversion function for angle
var speed:Number = 10;
//set speed
var xV:Number = Math.cos(radians) * speed;
//get x velocity
var yV:Number = Math.sin(radians) * speed;
//get y velocity
hero.x += xV;
if(hero.x < margin + heroCenter){
hero.x = margin + heroCenter;
}
if(hero.x > stage.stageWidth - (margin + heroCenter)){
hero.x = stage.stageWidth - (margin + heroCenter);
}
if(stage.mouseY > hero.y){
posNeg = -1;
} else {
posNeg = 1;
}
hero.y += yV*posNeg;
//move hero along new x velocity
if (hero.y > stage.stageHeight - (margin + heroCenter)) {
hero.y = stage.stageHeight - (margin + heroCenter);
} else if (hero.y < margin + heroCenter) {
hero.y = margin + heroCenter;
}
//move hero along new y velocity
}
function deg2rad(deg:Number):Number {
return deg * (Math.PI / 180);
//convert degrees to radians
}
stage.addEventListener(Event.ENTER_FRAME, cursorHold, false, 0, true);