2017-01-17 11 views
1

、私はこのコードは動作しません。どのようにしてソースを見つけるか? Screepsで

var sources = creep.room.find(Game.FIND_SOURCES_ACTIVE); 

それはこのことを言う:

Cannot read property 'find' of undefined 

私は周りを探してきたとソースを見つけるために、他の方法を見つけることができません。

また、他の人のコードのほとんどが機能しないことに気付きました。実際のゲームに入ればチュートリアルのコードも機能しなくなりました。私はオフに行くためにあなたの完全なコードを持っていないので、私はあなたの問題について完全に確認することはできません

var sources = creep.pos.findClosestByRange(Game.SOURCES); 

または

var sources = creep.pos.findClosestByPath(Game.SOURCES); 
+3

もっと文脈を提供できますか? 'クリープ'は 'クリープ'のインスタンスではないかもしれないので、 'creep.room'が定義されていない可能性があります。 –

答えて

0

私は、あなたが探していることだと思います1つの問題はcreepが定義されていない可能性があります。

あなたのコードのどこかに、creepを定義する必要があります。これは、forループがゲームや部屋の各クリープをループするようにします。

var roleMiner = require('role.miner') // role.miner being the module name for miner actions 
for(var name in Game.creeps) { 
    var creep = Game.creeps[name]; 
    // 
    // do whatever you wish with the current selected creep. 
    // 
    // most of the time you will call a module similar to what the tutorials suggest and put your actions for it in there 
    // 
    if(creep.memory.role == 'miner'){ 
     roleMiner.run(creep); // passes the current selected creep to the run function in the module 
    } 
} 

したがって、あなたのroleMinerモジュールには、鉱夫の行動を定義するものがあります。

var roleMiner = { 
    run: function(creep) { 
     // this one returns an array of the sources that are in the room with the creep 
     var sourcesRoom = creep.room.find(FIND_SOURCES); 

     // this one returns the source object which is closest to the creeps positon 
     var sourcesClose = creep.pos.findClosestByRange(FIND_SOURCES); 
    } 
} 
module.exports = roleMiner; 

これが役に立ちます。

+0

私はちょうどこれを得ます: TypeError:未定義の 'findClosestByPath'プロパティを読み取ることができません –

0

イム新しいプレーヤーは、私のコードが効率的であるかわからない、私は、findメソッドは次のようになると思う。

var sources = creep.room.find(FIND_SOURCES_ACTIVE)

クリープハーベスターするアクティブなリソースに行くだろう。

関連する問題