Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
0
Members

I am having trouble understanding prefabs and instantiation

A topic by fufroom created Nov 30, 2016 Views: 290 Replies: 1
Viewing posts 1 to 2

Hi there I am very new to SuperPowers, but I love what I have seen so far.

I am doing a sample game project to practice some skills before I use this engine for LD37.

I am trying to randomly place trees around my map inside a square area between x -15 &15 and y -15 & 15

I created a new Scene called `Plants` and added one actor inside which has a body and a sprite.

I created a new script (shown below) but it only spawns one tree (or maybe 50 overlapping trees?) Any help will be greatly appreciated!

class ScatterTrees extends Sup.Behavior {
  trees = [];
  treeCount = 50;
  awake() {
     for ( let i = 0; i < this.treeCount; i++){
      let newtree = Sup.appendScene("Plants", this.actor)[0];
      newtree.setLocalPosition(Sup.Math.Random.float(-15, 15),Sup.Math.Random.float(-15, 15), 1.1);
      this.trees.push(newtree);
    }  
  }
  update() {
  }
}
Sup.registerBehavior(ScatterTrees);
(+2)

I was able to get it working!

class ScatterTrees extends Sup.Behavior {
  trees = [];
  treeCount = 50;
  
  start() {
    for (var i = 0; i < this.treeCount; i++){
      Sup.appendScene("Plants")[0].arcadeBody2D.warpPosition(Sup.Math.Random.float(-5, 5),Sup.Math.Random.float(-5, 5));
    } 
  }

  update() {
  }
}

Sup.registerBehavior(ScatterTrees);