为什么阵列重置?

时间:2015-06-27 19:47:41

标签: php

我正在使用PocketMine为我正在创建的迷你游戏服务器编写一个类,但我不认为这会对我的问题产生影响。我有这个数组$alive,在调用$players时应该包含start()的内容。调用函数onDeath()时,数组打印为空。

为什么会这样?

namespace minigames\games;

use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\event\Listener;
use minigames\BaseGame;
use pocketmine\level\Position;

class Infected implements Listener, BaseGame
{
    private $alive = [];

    private $plugin;
    private $spawn;
    private $level;

    public function __construct($plugin)
    {
        $this->plugin = $plugin;
        $this->level = $this->plugin->getServer()->getLevelByName("infected");
        $this->spawn = new Position(133, 4,  128, $this->level);
    }

    public function start($players)
    {
        $this->alive = $players;
        foreach($players as $p)
        {
        $p->teleport($this->spawn);
        }
        $this->plugin->getServer()->getLogger()->info(var_dump($this->alive)); //prints out array with contents the same as $players
    }

    public function onDeath(PlayerDeathEvent $event)
    {
        $this->plugin->getServer()->getLogger()->info(var_dump($this->alive)); //array is empty
    }

    public function stop($winner)
    {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

我试过这只是为了检查

<?php
class Infected
{
    private $alive = [];

    public function start($players)
    {
        $this->alive = $players;
        var_dump($this->alive);
    }

    public function onDeath()
    {
        var_dump($this->alive);
    }
}

$infected = new Infected();
$infected->start(['a', 'b']);
$infected->onDeath();

返回正确的

array (size=2)
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
array (size=2)
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)

我担心您使用的是不同的物体。