--------初始问题已解决,但我有一个新的问题 - 请看下面---------------------- ----
在我的横向滚动游戏中,我正在尝试制作玩家射击的吹镖。当然,我希望飞镖从玩家开始,然后飞镖朝他们的方向移动。
但是,我有一个问题。在我的游戏中,我使用滚动功能将播放器放在屏幕中间。这是函数:
// 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()'方法,但我对如何在这种情况下使用它感到困惑。
如何在相对于玩家的正确位置创建飞镖?
我很感激任何帮助。谢谢!
------------------------------------------编辑和UPDATE --------------------------
编辑和更新:我在正确的位置创建了飞镖(靠近播放器 - 请参阅下面的评论)并且'movePlayerDarts()'函数会移动它们。但实际上我遇到了一个新问题。当玩家在射击飞镖后移动时,飞镖跟着他!如果玩家跳跃,飞镖会上升。如果玩家向左移动,则飞镖略微向左移动。
显然,有些代码告诉飞镖跟随玩家。我不知道如何,除非'playerDartContainer'与此有关。但是容器总是处于位置(0,0)并且它不会移动。
另外,作为测试,我在常量运行的'movePlayerDarts()'函数中跟踪了飞镖的'y'坐标。如您所见,该功能通过增加其y坐标值不断地使飞镖沿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);
}
}
答案 0 :(得分:1)
playerDartContainer 应与播放器实例处于同一级别进行实例化。如果它没有更新它相对于玩家的位置,那么实例化并添加到它的飞镖就会出现在任何地方。
首先解决方案是将 playerDartContainer 的位置设置为 playerDartContainer.x = player.x 和 playerDartContainer.y = player.y 无论你在哪里更新你的球员位置。将飞镖的实例化更改为0,0。
第二个解决方案是将位置设置为0,0(始终是屏幕的左上角),并将playerDart偏移到与playerDartContainer中的玩家相同的位置。由于你的玩家永远不会真正移动,所以playerDartContainer也不应该,所以你的飞镖会出现在与玩家相同的相对空间中。