Doromano's Forum Posts

  • 1 posts
  • Hello!

    I've got alot of similar sprites with Sprite_obj%number% name. They all have physics behavior.

    When for example Sprite_obj1 is on collision with another Sprite_obj1 I need them to be destroyed and create Sprite_obj2 instead.

    For Sprite_obj2 same but create Sprite_obj3

    Everything is ok as long as I use event "on collision" for each Sprite_obj.

    But when I try to use script, it only works when Sprite_objs hit each other hard.

    If I just place them on top of each other with couple of px between it wouldn't work.

    import GlobalVars from "./globalVars.js";
    
    export function Tick(runtime){
    
    	const dt = runtime.dt;
    	
    	HandleCollision(runtime, "Sprite_obj1", "Sprite_obj2");
    	HandleCollision(runtime, "Sprite_obj2", "Sprite_obj3");
    	HandleCollision(runtime, "Sprite_obj3", "Sprite_obj4");
    	HandleCollision(runtime, "Sprite_obj4", "Sprite_obj5");
    	
    	for (const starsInstance of runtime.objects.Stars.instances())
    	{
    		FadeStars(starsInstance, dt);
    	}
    }
    
    function HandleCollision(runtime, Sprite1, Sprite2) {
     const stars = runtime.objects.Stars;
    	const Sprite = runtime.objects[Sprite1];
    	const instances = Sprite.getAllInstances();
    	for (let i = 0; i < instances.length; i++) {
     for (let j = i + 1; j < instances.length; j++) {
     if (instances[i].testOverlap(instances[j])) {
    				const scoreInstance = runtime.objects.TestText.getFirstInstance();
    				scoreInstance.text = GlobalVars.test;
    				const newSprite = runtime.objects[Sprite2].createInstance("Game", instances[i].x, instances[i].y);
    				const starsInstance = stars.createInstance("Game", newSprite.x, newSprite.y);
    				starsInstance.isVisible = true;
    				starsInstance.angleDegrees = runtime.random() * 360;
     instances[i].destroy();
     instances[j].destroy();
     }
     }
     }
    }
    

    Can anyone help me with this?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 1 posts