JS Basics - Instance and set position (with linked file)

  • Hi I am trying to learn basic js with construct - in the attached file, how would I instance player1 100 times, and set each instance to a 10 x 10 grid?

    thanks much

    drive.google.com/open

  • Hi Jsea, I'm new to C3 so I may not be the best person to answer your question, but try adding this to your for loop:

    const randomNumberX = runtime.random() * 450;
    const randomNumberY = runtime.random() * 450;
    
    runtime.objects.player.createInstance(0, randomNumberX, randomNumberY);
    	
    // Or
    	
    // const clone = runtime.objects.player.createInstance(0, 0, 0);
    	
    // clone.x = runtime.random() * 450;
    // clone.y = runtime.random() * 450;
    
  • thanks! heres my grid! It's gorgeous!

    If I have one object called enemy, how do I get/set position? I tried

    runtime.objects.enemy.x,runtime.objects.enemy.y but it gives me undefined.

    thanks!

    runOnStartup(async runtime =>
    {
    	runtime.addEventListener("beforeprojectstart",() => OnBeforeProjectStart(runtime));
    
    });
    
    function OnBeforeProjectStart(runtime)
    {
    	runtime.layout.addEventListener("beforelayoutstart",() => OnBeforeLayoutStart(runtime));
    	
    }
    
    function OnBeforeLayoutStart(runtime)
    {
    	
    	console.log(runtime.objects.enemy.x,runtime.objects.enemy.y);
    	
    	for(var i = 0;i<10;i++){
    		for(var j = 0;j<10;j++){
    		runtime.objects.player.createInstance(0,i*32+32,j*32+32);
    		}
    
    	}
    	
    }
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Glab I could help! Your grid is magnificent ; )

    To set and get the position of an enemy you could try this.

    runOnStartup(async runtime =>
    {
    	runtime.addEventListener("beforeprojectstart",() => OnBeforeProjectStart(runtime));
    });
    
    function OnBeforeProjectStart(runtime)
    {
    	runtime.layout.addEventListener("beforelayoutstart",() => OnBeforeLayoutStart(runtime));
    }
    
    function OnBeforeLayoutStart(runtime)
    {
    	//g_playerInstance = runtime.objects.Player.getFirstInstance();
    	var Player1 = runtime.objects.player.getFirstInstance();
    // 	console.log(Player1.x);
    	for(var i = 0;i<100;i++){
    // 	console.log(i);
    
    // 		runtime.objects.player.createInstance(0, runtime.random() * 1280, runtime.random() * 960);
    
    	const randomNumberX = runtime.random() * 450;
    	const randomNumberY = runtime.random() * 450;
    
    	runtime.objects.player.createInstance(0, randomNumberX, randomNumberY);
    	
    	// Or
    	
    // 	const clone = runtime.objects.player.createInstance(0, 0, 0);
    	
    // 	clone.x = runtime.random() * 450;
    // 	clone.y = runtime.random() * 450;
    	}
    	
    	const enemyOne = runtime.objects.enemy.createInstance(0, 475, 275);
    	
    	const enemyOneX = enemyOne.x;
    	const enemyOneY = enemyOne.y;
    	
    	console.log("Enemy X: " + enemyOneX + ", Enemy Y: " + enemyOneY);
    }
    
  • Insane!

    Thanks DFORM!

  • You're welcome.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)