Skip to main content
This topic has been closed for replies.

3 replies

JoãoCésar17023019
Community Expert
Community Expert
May 14, 2022

As far as I can tell there's no way to randomize the instances and their properties in each playback.

 

The only solution I can think of is to randomize some instances and properties in each keyframe.

 

My suggestion here is to run one script to spawn and randomize the instances and another one to randomize their properties in each keyframe.

 

Here is a video demonstration:

https://bit.ly/3l92koz

 

Spawn Symbol Randomly.jsfl

var dom = fl.getDocumentDOM();
var lib = dom.library;
var timeline = dom.getTimeline();
var symbolName = "Circle";
var total = 16;
var minX = 0;
var maxX = dom.width;
var minY = 0;
var maxY = dom.height;
var minScale = 0.1;
var maxScale = 1;
var minRotation = 0;
var maxRotation = 360;
var minAlpha = 20;
var maxAlpha = 100;

function main()
{	
	if (!checkForErrors())
	{
		getUserInput();
		randomSpawn();
	}
}

function checkForErrors()
{
	var currentLayer = timeline.layers[timeline.currentLayer];
	var selectedFrames = timeline.getSelectedFrames();
	
	if (!dom)
	{
		alert("Please open up a FLA first.");
		return true;
	}

	if (currentLayer.layerType === "folder")
	{
		alert("The current layer is a folder.");
		return true;
	}

	if (currentLayer.locked)
	{
		alert("The current layer is locked.");
		return true;
	}

	if (selectedFrames.length === 0)
	{
		alert("There's no selected frame.");
		return true;
	}

	if (selectedFrames[2] > currentLayer.frames.length)
	{
		alert("Selected frame is null.");
		return true;
	}

	return false;
}

function getUserInput()
{
	var userInput, totalInput;
	var data = prompt("Please input the Symbol name and the number of instances. E.g.: 'Circle,32.' or 'anims/Player,10.'");
	
	if (data)
	{
		userInput = data.split(",");
		
		if (userInput.length > 1)
		{
			totalInput = parseInt(userInput[1]);
			symbolName = userInput[0];
			total = isNaN(totalInput) ? total : totalInput;	
		}
	}
}

function randomSpawn()
{
	var i, randomScale, randomRotation;
	
	for (i = 0; i < total; i++)
	{
		lib.addItemToDocument({ x: randomRange(minX, maxX), y: randomRange(minY, maxY) }, symbolName);
		randomScale = randomRange(minScale, maxScale);
		dom.transformSelection(randomScale, 0, 0, randomScale);
		dom.rotateSelection(randomRange(minRotation, maxRotation));
		dom.setInstanceAlpha(randomRange(minAlpha, maxAlpha));
	}
}

function randomRange(min, max)
{
	return min + Math.random() * (max - min);
}

fl.outputPanel.clear();
main();

 

Randomize Instances.jsfl

var dom = fl.getDocumentDOM();
var lib = dom.library;
var timeline = dom.getTimeline();

var minOffsetX = -50;
var maxOffsetX = 50;
var minOffsetY = -30;
var maxOffsetY = 30;

var minOffsetScale = 0.02;
var maxOffsetScale = 0.02;

var minOffsetRotation = 30;
var maxOffsetRotation = 80;

var minAlpha = 20;
var maxAlpha = 100;

function main()
{	
	if (!checkForErrors())
		randomize();
}

function checkForErrors()
{
	if (!dom)
	{
		alert("Please open up a FLA first.");
		return true;
	}

	if (dom.selection.length === 0)
	{
		alert("There's no instance selected.");
		return true;
	}

	return false;
}

function randomize()
{
	var selection = dom.selection;
	var total = selection.length;
	var element;
	
	dom.selectNone();
	
	for (i = 0; i < total; i++)
	{
		element = selection[i];
		element.selected = true;
		element.x += randomRange(minOffsetX, maxOffsetX);
		element.y += randomRange(minOffsetY, maxOffsetY);
		element.rotation += randomRange(minOffsetRotation, maxOffsetRotation);
		element.scaleX += randomRange(minOffsetScale, maxOffsetScale);
		element.scaleY = element.scaleX;
		dom.setInstanceAlpha(randomRange(minAlpha, maxAlpha));
		dom.selectNone();
	}

	for (i = 0; i < total; i++)
	{
		element = selection[i];
		element.selected = true;
	}
}

function randomRange(min, max)
{
	return min + Math.random() * (max - min);
}

fl.outputPanel.clear();
main();

 

Here is the source code / files / JSFL:

https://bit.ly/37JVPFq

 

I hope it helps.

 

Regards,

JC

kqskcmAuthor
Known Participant
May 15, 2022

Thank you very much for your answer, but you may have misunderstood me。I hope to select many symbols at the same time, execute jsfl, so that the starting frame of each symbol playback is different, so as to realize irregular playback。

JoãoCésar17023019
Community Expert
Community Expert
May 14, 2022

Hi.

 

Do you want to pre-randomize the animations before publish (authortime) or after publish (runtime)?

 

Regards,

JC

kqskcmAuthor
Known Participant
May 14, 2022

before publish,Play the effect on the timeline

kqskcmAuthor
Known Participant
May 14, 2022

Currently only zoom effects can be done at the same time