Jump to content
GreenSock

witochka

Question: Adobe Animate - Random Movement

Recommended Posts

Greetings guys,

 

I'm fairly new to GreenSock and Coding, like everyone who is just getting started, I quickly arrived at a point where I don't know how to continue:

 

I'm working in Adobe Animate and I wanted my movieclip hexagon (InstName = hexagon) to move randomly on my canvas. 

As an end goal, I wanted several hexagons to move around randomlymaybe even change in size, and I wanted it to play infinitely (screenshots attached).

 

I found an old tutorial with some code and tried to apply it, but it didn't work. Can anyone tell me why? Is it because I'm using jQuery?

 

Scripts included:

https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js

http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js

 

Code:

 

var randX;
var randY;

function randomNumbers(){
    
    randX = Math.floor(Math.random()*100);
    randY = Math.floor(Math.random()*150);
    moveMe();
}

randomNumbers();

function moveMe(){
    sym.$("hexagon").animate({left: randX, top: randY},1000, randomNumbers);
}

 

 

How can I achieve my goal? Or can anyone please send me a link where a similar topic is solved and discussed?

 

Any help would be greatly appreciated!

Bildschirmfoto 2018-01-24 um 10.30.25.png

Bildschirmfoto 2018-01-24 um 10.30.47.png

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums,

 

jQuery isn't going to work inside Animate CC. jQuery is for working with DOM elements (div, span, img, etc) and Animate is all canvas using the EaselJS library for rendering.

 

The simplest way for constant random motion is to create a function that creates a tween that moves something randomly. When that tween is finished you call the same function again and tell it to move the same thing again to other random coordinates.

 

The code for 2 objects to move randomly and independently looks like this:

function moveIt(it) {
 TweenLite.to(it, (Math.random() * 2) + 0.5, {x:Math.random() * 400, y:Math.random() * 400, onComplete:moveIt, onCompleteParams:[it]});    
}
    
moveIt(this.box);
moveIt(this.box2);

 

 

demo: https://greensock.com/forums-support-files/randomMovement/randomMovement.html

 

I have attached a zip of the files.

 

 

randomMovement.zip

  • Like 3
Link to comment
Share on other sites

Hello Carl,

 

i can`t thank you enough. It's so important to get some help in the beginning to stay motivated and not give up.

Thanks a lot for your help and time. I really appreciate it.

 

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×