【问题标题】:Can't edit object variable from a seperate script无法从单独的脚本编辑对象变量
【发布时间】:2019-02-16 06:19:57
【问题描述】:

我只是想从我的脚本中更改一个简单对象的变量。代码运行,但没有改变变量。

编辑这个变量应该会降低敌人的生命值,但不会。如果我从对象本身编辑此变量,健康栏会发生变化。

enemies();
friends();
randomize();

//get enemy from array and make an instance
active_enemy = enemy_list[irandom_range(0, 1)];

var inst1 = instance_create_depth(200, 75, 1, active_enemy); 

//get friend from arrayand make an instance
active_friend = friend_list[irandom_range(0, 1)];

var inst2 = instance_create_depth(96, 175, 1, active_friend); 

//change variable
inst1.e_health_active = 1;

此脚本放置在战斗室创建代码中,并且 e_health_active 作为其统计数据的一部分存在于每个敌人的 obj 代码中。

谢谢!

【问题讨论】:

    标签: game-maker game-maker-language game-maker-studio-2


    【解决方案1】:

    如果您通过在放置在起始房间的持久Game 对象中执行此global.e_health_active = 1; 使敌人的健康变量成为全局变量,则global.e_health_active 现在可以在程序中的任何位置访问。它不再必须在同一个对象中。

    做这样的事情:

    // Persistent game object - where you would like to store all your global variables
    // Create event 
       global.e_health_active = 10; // this can be any number you want it to be. 
    

    然后将以下内容放入战斗室创建代码中

    // Creation code of battle room 
    enemies();
    friends();
    randomize();
    
    //get enemy from array and make an instance
    active_enemy = enemy_list[irandom_range(0, 1)];
    
    var inst1 = instance_create_depth(200, 75, 1, active_enemy); 
    
    //get friend from arrayand make an instance
    active_friend = friend_list[irandom_range(0, 1)];
    
    var inst2 = instance_create_depth(96, 175, 1, active_friend); 
    
    //change variable
    inst1.global.e_health_active = 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-12
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 2016-07-27
      • 2018-08-30
      相关资源
      最近更新 更多