【发布时间】:2015-03-02 00:19:38
【问题描述】:
我想让我的播放器在几秒钟内提高速度。当它收集 4 个项目 (paintCount = 4) 时,玩家会在短时间内获得移动速度提升。
我在 Paintser 类中总是有一个错误:SimplePlayer0.SpeedUp();。
我已经尝试了很多方法来对抗它,但它们都不起作用。
我在 Unity 工作。
错误:非静态字段、方法或属性“SimplePlayer0.SpeedUp() 需要对象引用。
这是播放器脚本:
using UnityEngine;
using System.Collections;
public class SimplePlayer0 : MonoBehaviour
{
// SPEEDVARIABLES
public static float speed = 3.5f;
// BONUSSPEED
private static float speedBoostTime;
public static float SpeedBoostTime
{
get
{
return speedBoostTime;
}
set
{
speedBoostTime = value;
}
}
// BONUSSPEED
public void SpeedUp()
{
speed *= 2;
SpeedBoostTime = 3; // seconds
}
void Update()
{
// BONUSSPEED
while (speedBoostTime > 0)
{
speedBoostTime -= Time.deltaTime;
if (speedBoostTime <= 0) speed /= 2;
}
}
这是游戏对象被销毁的加电脚本。
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Paintser.ExtraTime();
Destroy(this.gameObject);
Paintser.paintCount++;
}
}
}
最后是所有魔法(或错误)发生的脚本:
using UnityEngine;
using System.Collections;
public class Paintser : PowerUp
{
public static int paintCount = 0;
public int speedBoostTime = 3;
public static void ExtraTime()
{
if (paintCount == 4)
{
SimplePlayer0.SpeedUp();
Paintser.paintCount = Paintser.paintCount = 0;
}
}
}
【问题讨论】: