在 as3 中移动一个对象相对于另一个对象
好的,我有一个在鼠标单击时移动的 character_mc、一个 logtxt 动态文本框和一个名为 $box 的 mc。当日志和 $ 框保持原状时,摄像机会跟随角色。
我希望他们留在舞台上的同一点(意味着我希望他们留在相机上的同一位置)我想知道我可以为此做什么?我曾尝试将它们放入电影剪辑中,但效果并不理想,所以我放弃了这个想法。
代码如下:
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Stage;
public class Main extends MovieClip {
// player
public var characterEndMC:MovieClip = new charEndMC();
//box
private var boxAmount:Number=0;
private var boxLimit:Number=20;
private var _root:Object;
//$txt
public var money:int=0;
public var gold:int=0;
//$$
public var testnumber:Number=1;
//enemy1
private var e01Amount:Number=0;
private var e01Limit:Number=2;
public function Main() {
$box.click$.move$.buttonMode=true;
$box.click$.clickmini$.buttonMode=true;
character_mc["charAngle"]=0;
character_mc["moveX"]=0;
character_mc["moveY"]=0;
character_mc["walkSpeed"]=7;
stage.addEventListener(MouseEvent.CLICK, charMove);
//box add listener
addEventListener(Event.ENTER_FRAME, eFrame);
//moneybox
$box.click$.move$.addEventListener(MouseEvent.MOUSE_DOWN, startmoving$);
$box.click$.move$.addEventListener(MouseEvent.MOUSE_UP, stopmoving$);
$box.click$.clickmini$.addEventListener(MouseEvent.CLICK, c$mini);
}
public function charLoop(event:Event) {
if (character_mc.hitTestPoint(character_mc["charEnd"].x,character_mc["charEnd"].y,true)) {
character_mc["moveX"]=0;
character_mc["moveY"]=0;
this.removeChild(character_mc["charEnd"]);
character_mc.removeEventListener(Event.ENTER_FRAME, charLoop);
}
for (var i:int = 0; i < this.numChildren; i++) {
this.getChildAt(i).x-=character_mc["moveX"];
this.getChildAt(i).y-=character_mc["moveY"];
}
character_mc.x+=character_mc["moveX"]+.05;
character_mc.y+=character_mc["moveY"]+.05;
}
public function lookAtMouse() {
var characterMC:MovieClip=character_mc;
characterMC["charAngle"] = Math.atan2(this.mouseY - characterMC.y, this.mouseX - characterMC.x) / (Math.PI / 180);
characterMC.rotation=characterMC["charAngle"];
}
public function charMove(event:MouseEvent) {
lookAtMouse();
this.addChild(characterEndMC);
characterEndMC.x=this.mouseX;
characterEndMC.y=this.mouseY;
character_mc["charEnd"]=characterEndMC;
character_mc["charEnd"].visible = false;
character_mc["moveX"]=Math.cos(character_mc["charAngle"]*Math.PI/180)*character_mc["walkSpeed"];
character_mc["moveY"]=Math.sin(character_mc["charAngle"]*Math.PI/180)*character_mc["walkSpeed"];
character_mc.addEventListener(Event.ENTER_FRAME, charLoop);
}
//boxadding
private function eFrame(event:Event):void {
if (boxAmount<boxLimit) {
boxAmount++;
var _box:Box=new Box ;
_box.addEventListener(MouseEvent.CLICK,boxclick);
_box.buttonMode=true;
_box.y=Math.random()* (1000 + _box.height/2);
_box.x=Math.random()* (1000 + _box.width/2);
addChild(_box);
}
if (e01Amount<e01Limit) {
e01Amount++;
var Enemy1:enemy01=new enemy01 ;
Enemy1.addEventListener(MouseEvent.CLICK, en01click);
Enemy1.buttonMode=true;
Enemy1.y=Math.random()*stage.stageHeight;
Enemy1.x=Math.random()*stage.stageWidth;
addChild(Enemy1);
}
}
public function boxclick(event:MouseEvent):void {
var _box:Box=event.currentTarget as Box;
logtxt.appendText("You collected " + testnumber + " boxes" + "\n" );
character_mc["moveX"] = _box.y + 40 + (height / 2);
character_mc["moveY"]=_box.x;
logtxt.scrollV=logtxt.maxScrollV;
var randVal$:Number=Math.random();
if (randVal$>=0.49) {
money+=100;
} else if (randVal$ <= 0.50 && randVal$ >= 0.15) {
money+=200;
} else if (randVal$ <= 0.14 && randVal$ >= 0.02) {
gold+=10;
} else if (randVal$ == 0.01) {
money+=200;
gold+=20;
}
testnumber++;
boxAmount--;
$box.box$in.box$insins.Moneytxt.text=String(money);
$box.box$in.box$insins.Goldtxt.text=String(gold);
removeChild(_box);
}
private function startmoving$(event:MouseEvent):void {
$box.startDrag();
}
private function stopmoving$(event:MouseEvent):void {
$box.stopDrag();
}
private function c$mini(event:MouseEvent):void {
$box.click$.move$.visible=false;
$box.box$in.visible=false;
$box.y=200;
$box.x=100;
$box.click$.clickmini$.addEventListener(MouseEvent.CLICK, reclickbox$);
$box.click$.clickmini$.removeEventListener(MouseEvent.CLICK, c$mini);
}
private function reclickbox$(event:MouseEvent):void {
$box.click$.clickmini$.addEventListener(MouseEvent.CLICK, c$mini);
$box.click$.clickmini$.removeEventListener(MouseEvent.CLICK, reclickbox$);
$box.y=70;
$box.x=250;
$box.click$.move$.visible=true;
$box.box$in.visible=true;
}
public function scroll_text( n:Number ) {
logtxt.scrollV = Math.round( ( logtxt.maxScrollV - 1 ) * n ) + 1;
}
public function en01click(event:MouseEvent) {
}
}
}
Ok so i have a character_mc that moves on mouse click, a logtxt dyanamic text box and a mc named $box. The camera follows the character and when the log and $ boxes are staying put.
I want them to stay on the same point on the stage(meaning I want them to stay at the same place on the camera) I was wondering what i might do for this? I have tried to put them in a movie clip but it doesn't really seam like it worked well so i discarded the idea.
Heres the code:
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Stage;
public class Main extends MovieClip {
// player
public var characterEndMC:MovieClip = new charEndMC();
//box
private var boxAmount:Number=0;
private var boxLimit:Number=20;
private var _root:Object;
//$txt
public var money:int=0;
public var gold:int=0;
//$
public var testnumber:Number=1;
//enemy1
private var e01Amount:Number=0;
private var e01Limit:Number=2;
public function Main() {
$box.click$.move$.buttonMode=true;
$box.click$.clickmini$.buttonMode=true;
character_mc["charAngle"]=0;
character_mc["moveX"]=0;
character_mc["moveY"]=0;
character_mc["walkSpeed"]=7;
stage.addEventListener(MouseEvent.CLICK, charMove);
//box add listener
addEventListener(Event.ENTER_FRAME, eFrame);
//moneybox
$box.click$.move$.addEventListener(MouseEvent.MOUSE_DOWN, startmoving$);
$box.click$.move$.addEventListener(MouseEvent.MOUSE_UP, stopmoving$);
$box.click$.clickmini$.addEventListener(MouseEvent.CLICK, c$mini);
}
public function charLoop(event:Event) {
if (character_mc.hitTestPoint(character_mc["charEnd"].x,character_mc["charEnd"].y,true)) {
character_mc["moveX"]=0;
character_mc["moveY"]=0;
this.removeChild(character_mc["charEnd"]);
character_mc.removeEventListener(Event.ENTER_FRAME, charLoop);
}
for (var i:int = 0; i < this.numChildren; i++) {
this.getChildAt(i).x-=character_mc["moveX"];
this.getChildAt(i).y-=character_mc["moveY"];
}
character_mc.x+=character_mc["moveX"]+.05;
character_mc.y+=character_mc["moveY"]+.05;
}
public function lookAtMouse() {
var characterMC:MovieClip=character_mc;
characterMC["charAngle"] = Math.atan2(this.mouseY - characterMC.y, this.mouseX - characterMC.x) / (Math.PI / 180);
characterMC.rotation=characterMC["charAngle"];
}
public function charMove(event:MouseEvent) {
lookAtMouse();
this.addChild(characterEndMC);
characterEndMC.x=this.mouseX;
characterEndMC.y=this.mouseY;
character_mc["charEnd"]=characterEndMC;
character_mc["charEnd"].visible = false;
character_mc["moveX"]=Math.cos(character_mc["charAngle"]*Math.PI/180)*character_mc["walkSpeed"];
character_mc["moveY"]=Math.sin(character_mc["charAngle"]*Math.PI/180)*character_mc["walkSpeed"];
character_mc.addEventListener(Event.ENTER_FRAME, charLoop);
}
//boxadding
private function eFrame(event:Event):void {
if (boxAmount<boxLimit) {
boxAmount++;
var _box:Box=new Box ;
_box.addEventListener(MouseEvent.CLICK,boxclick);
_box.buttonMode=true;
_box.y=Math.random()* (1000 + _box.height/2);
_box.x=Math.random()* (1000 + _box.width/2);
addChild(_box);
}
if (e01Amount<e01Limit) {
e01Amount++;
var Enemy1:enemy01=new enemy01 ;
Enemy1.addEventListener(MouseEvent.CLICK, en01click);
Enemy1.buttonMode=true;
Enemy1.y=Math.random()*stage.stageHeight;
Enemy1.x=Math.random()*stage.stageWidth;
addChild(Enemy1);
}
}
public function boxclick(event:MouseEvent):void {
var _box:Box=event.currentTarget as Box;
logtxt.appendText("You collected " + testnumber + " boxes" + "\n" );
character_mc["moveX"] = _box.y + 40 + (height / 2);
character_mc["moveY"]=_box.x;
logtxt.scrollV=logtxt.maxScrollV;
var randVal$:Number=Math.random();
if (randValgt;=0.49) {
money+=100;
} else if (randVal$ <= 0.50 && randVal$ >= 0.15) {
money+=200;
} else if (randVal$ <= 0.14 && randVal$ >= 0.02) {
gold+=10;
} else if (randVal$ == 0.01) {
money+=200;
gold+=20;
}
testnumber++;
boxAmount--;
$box.box$in.box$insins.Moneytxt.text=String(money);
$box.box$in.box$insins.Goldtxt.text=String(gold);
removeChild(_box);
}
private function startmoving$(event:MouseEvent):void {
$box.startDrag();
}
private function stopmoving$(event:MouseEvent):void {
$box.stopDrag();
}
private function c$mini(event:MouseEvent):void {
$box.click$.move$.visible=false;
$box.box$in.visible=false;
$box.y=200;
$box.x=100;
$box.click$.clickmini$.addEventListener(MouseEvent.CLICK, reclickbox$);
$box.click$.clickmini$.removeEventListener(MouseEvent.CLICK, c$mini);
}
private function reclickbox$(event:MouseEvent):void {
$box.click$.clickmini$.addEventListener(MouseEvent.CLICK, c$mini);
$box.click$.clickmini$.removeEventListener(MouseEvent.CLICK, reclickbox$);
$box.y=70;
$box.x=250;
$box.click$.move$.visible=true;
$box.box$in.visible=true;
}
public function scroll_text( n:Number ) {
logtxt.scrollV = Math.round( ( logtxt.maxScrollV - 1 ) * n ) + 1;
}
public function en01click(event:MouseEvent) {
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的是使用 Sprites 将所有相关对象放在自己的图层上。
理解你在示例中所做的事情有点困难,但这里是
您可以做什么的简单示例:
What you can do is put all related objects on their own layer using Sprites.
It's a bit difficult to understand what you are doing in your example but here's
a quick example of what you can do :