本文介绍了错误的函数行为,错在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遇到的问题是这样的:



战士向导每个 魔力 武器力量。我试图创造一个限制,以便你可以在任何一个Power中花费最多25点。发生的事情是它不会让你在剩下的力量中花费剩下的5分。最重要的是,它同时适用于战士和向导,它不应该是。每个人都应该有25个人的限制。

供参考:我也对大国进行了限制,以便他们只能达到预期的75。 >

以下是

解决方案
由于@JAL指出 ptsSpent 是一个全局变量。错误发生在30行和32行。



if(pointsVal.value == 0 || newNumber> 75 || ptsSpent> 24 )return;


  1. 考虑使用向导和warrior作为实例来构建一个'Character'类这个例子)。 ptsSpent 将是该类的一个私有变量。

  2. 您链接的控制台列出了9个警告。你也可以考虑解决这些问题。干净的编译只是一个好的编程习惯,警告是有原因的。 :




基本上 ptsSpent 需要以与武器魔术 stat变量相同的方式实现。这些变量由各个对象拥有。


I have created a tool that will be used in character creation for a video game.

The problem I'm having is this:

The Warrior and Wizard have 30 points each to spend on Magic Power or Weapon Power. I tried to create a limit so that you can spend a maximum of 25 points into either Power. What's happening is it won't let you spend the remaining 5 points in the other Power. On top of that, it counts for both Warrior and Wizard and it should not be. Each should have individual limits of 25.

FYI: I also put a limit on the Powers so that they can only go up to 75, which is intended.

Here is the CODE AND DEMO

解决方案

As @JAL states ptsSpent is a global variable. The error occurs on Lines 30 and 32.

if(pointsVal.value == 0 || newNumber > 75 || ptsSpent > 24) return;

  1. Consider building a 'Character' class with wizard and warrior being instances (in the scope of this example). ptsSpent would be a private-variable of the class.
  2. The console you linked to lists "9 warnings". You might look to resolving those as well. Just good coding practice to compile cleanly, warnings are there for a reason. :)

[edit]Basically ptsSpent needs to be implemented in the same manner as weapon and magic stat variables already are. Those variables are owned by the individual objects.

这篇关于错误的函数行为,错在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 14:24