【发布时间】:2011-11-07 00:32:17
【问题描述】:
-------- 最初的问题已修复,但我有一个新问题 - 请参见下文 ---------- ----
在我的横向卷轴游戏中,我尝试制作玩家射击的吹镖。当然,我希望飞镖从玩家所在的位置开始,然后飞镖朝他们的方向移动。
但是,我有一个问题。在我的游戏中,我使用了一个滚动功能,将玩家放在屏幕中间。这是那个函数:
// This function scrolls the MovieClip Containers on the screen
private function scrollScreen(event:Event):void {
// Here we offset the elements' 'x' and 'y' coordinates by
// the distance between the Player and the centre of the stage
// Environment Container
envContainer.x += (stage.stageWidth * 0.5) - player.x;
// Background Container
// Background moves more slowly along the 'x' axis to simulate distance
bgContainer.x += ((stage.stageWidth * 0.5) - player.x) * 1/20;
// Position the Player at the centre of the game screen ('x' axis)
player.x = stage.stageWidth * 0.5;
// Here we offset the elements' 'y' coordinates
// Environment Container
envContainer.y += (stage.stageHeight * 0.5) - player.y;
// Background Container
bgContainer.y += (stage.stageHeight * 0.5) - player.y;
// Position the Player at the centre of the game screen ('y' axis)
player.y = stage.stageHeight * 0.5;
} // End of 'scrollScreen()' function
据我所知,“envContainer”和“bgContainer”上的滚动与我正在谈论的问题无关。我只关心玩家的定位。
下面是我用来创建飞镖的函数:
// This function creates a dart
private function createDart():void {
if (playerDartContainer.numChildren <= 4) {
trace("DART!");
playerDart = new PlayerDart();
playerDart.x = player.x;
playerDart.y = player.y;
trace("PD x: " + playerDart.x);
trace("Player x: " + player.x);
trace("PD y: " + playerDart.y);
trace("Player y: " + player.y);
// Set the new dart's direction to equal that of the player
// Player's facing right
if (player.scaleX == 1) {
// So dart faces right, too
playerDart.scaleX = 1;
}
// Player's facing left
else if (player.scaleX == -1) {
// So dart faces left, too
playerDart.scaleX = -1;
}
playerDartContainer.addChild(playerDart);
trace("Num children: " + playerDartContainer.numChildren);
}
}
发生的情况是飞镖总是添加到舞台的点 (350, 350) - 玩家放置的确切点(相对于屏幕,即 700*700)。所以我知道问题与程序认为 Player 和 Dart 对象应该是的坐标有关。我只是不知道如何解决这个问题。我看过 'globalToLocal()' 方法,但我对如何在这种情况下使用它感到困惑。
如何在相对于播放器的正确位置创建飞镖?
如果有任何帮助,我将不胜感激。谢谢!
------------------------------------------ 编辑和更新--------------------------
编辑和更新:我在正确的位置(靠近玩家——见下面的 cmets)创建了飞镖,并使用“movePlayerDarts()”函数移动它们。但我实际上有一个新问题。当玩家发射飞镖后移动时,飞镖会跟随他!如果玩家跳跃,飞镖就会升起。如果玩家向左移动,飞镖会稍微向左移动。
显然,某处有一些代码告诉飞镖跟随玩家。我不明白如何,除非“playerDartContainer”与此有关。但是容器始终在位置 (0,0) 并且它不会移动。
此外,作为测试,我在不断运行的“movePlayerDarts()”函数中追踪了飞镖的“y”坐标。如您所见,该函数通过增加 y 坐标值不断将 dart 沿 y 轴移动。但是当我跳跃时,被追踪的 'y' 坐标永远不会减少,即使飞镖看起来像是在上升!
如果有人有任何建议,我将不胜感激!
这是我用来创建飞镖的代码:
// This function creates a dart
public function createDart():void {
if (playerDartContainer.numChildren <= 4) {
// Play dart shooting sound
sndDartShootIns.play();
// Create a new 'PlayerDart' object
playerDart = new PlayerDart();
// Set the new dart's initial position and direction depending on the player's direction
// Player's facing right
if (player.scaleX == 1) {
// Create dart in front of player's dart gun
playerDart.x = player.x + 12;
playerDart.y = player.y - 85;
// Dart faces right, too
playerDart.scaleX = 1;
}
// Player's facing left
else if (player.scaleX == -1) {
// Create dart in front of player's dart gun
playerDart.x = player.x - 12;
playerDart.y = player.y - 85;
// Dart faces left, too
playerDart.scaleX = -1;
}
playerDartContainer.addChild(playerDart);
}
} // End of 'createDart()' function
此代码是玩家飞镖的 EnterFrameHandler:
// In every frame, call 'movePlayerDarts()' to move the darts within the 'playerDartContainer'
public function playerDartEnterFrameHandler(event:Event):void {
// Only move the Player's darts if their container has at least one dart within
if (playerDartContainer.numChildren > 0) {
movePlayerDarts();
}
}
最后,这是实际移动所有玩家飞镖的代码:
// Move all of the Player's darts
public function movePlayerDarts():void {
for (var pdIns:int = 0; pdIns < playerDartContainer.numChildren; pdIns++) {
// Set the Player Dart 'instance' variable to equal the current PlayerDart
playerDartIns = PlayerDart(playerDartContainer.getChildAt(pdIns));
// Move the current dart in the direction it was shot. The dart's 'x-scale'
// factor is multiplied by its speed (5) to move the dart in its correct
// direction. If the 'x-scale' factor is -1, the dart is pointing left (as
// seen in the 'createDart()' function. (-1 * 5 = -5), so the dart will go
// to left at a rate of 5. The opposite is true for the right-ward facing
// darts
playerDartIns.x += playerDartIns.scaleX * 1;
// Make gravity work on the dart
playerDartIns.y += 0.7;
//playerDartIns.y += 1;
// What if the dart hits the ground?
if (HitTest.intersects(playerDartIns, floor, this)) {
playerDartContainer.removeChild(playerDartIns);
}
//trace("Dart x: " + playerDartIns.x);
trace("Dart y: " + playerDartIns.y);
}
}
【问题讨论】:
标签: flash actionscript-3