Motions with tweens one after another in Actionscript? - actionscript

I have some problems with motions and tweens:
I want move my sprite to to the bottom right corner (800,600) then to the top left corner (0,0). But my tweens arent waiting each other.
motion.toBotCorner(currentSprite);
motion.toTopCorner(currentSprite);
And this is in my Motion class:
public function toBotCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:800, y:600});
}
public function toTopCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:0, y:0});
}
How to make the first one proccess and then the second? Thank you!

You should use the 'onComplete' provided by TweenLite on your first animation. It requires a method name, and use the 'onCompleteParams' to send the parameters to the method call.
So, your code would look like this now:
public function toBotCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:800, y:600, onComplete:toTopCorner, onCompleteParams:[currSpr]});
}
public function toTopCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:0, y:0});
}
Note that the onCompleteParams: is an array as a method could have multiple parameters to be passed.
Here's what the docs says:
onComplete : Function - A function that should be called when the tween has completed
onCompleteParams : Array - An Array of parameters to pass the onComplete function.
Hope this helps. Please accept this answer if it works for you, that would close the question. Thanks!

Related

How to construct std::wstring_view from two std::wstring::const_iterator-s?

What is the right way to implement the following function that creates std::wstring_view from two std::wstring iterators (pointing to the same std::wstring)?
std::wstring_view MakeStringView(std::wstring::const_iterator begin, std::wstring::const_iterator end)
{
//How to implement it?
}

Swift: Random number arrays inside images and variables with a for loop

I am creating a game in which, depending on the number of 'swipes' chosen to do, (let's say 3), 3 different patterns show on the screen, one by one. I am working on developing the first pattern.
So I have this:
if (swipes.no_of_swipes) == 3 {
swipeArray = Array<UInt32>(count: 3, repeatedValue: 0)
for i in 0 ..< 3 {
swipeArray[i] = arc4random_uniform(84)}
}
As far as I am aware, this code creates an array with three UInts which can be accessed by doing swipeArray[0], swipeArray[1], and swipeArray[2]. My first question is how long will this swipeArray stay the same? Until the close the view? Should I have a 'refresh button' when the user loses - and if so, how would I make one?
Then I have a property observer. You will notice the for loop, which I am using to keep code concise. I understand that I could do something like x++ somewhere in here so that it will go through each one.
var playBegin: Bool = false{
didSet {
if playBegin == true {
println("\(playBegin)")
var swipes = Menu()
if (swipes.no_of_swipes) == 3 {
for i in 0 ..< 3 {
patternRoom.image = UIImage(named: "pattern\(swipeArray[x])")
//rest of code
}
}
}
The pattern image comes from a set of 84 images named like pattern7 and pattern56. My second question is, how could I code the for loop to go through each swipeArray[x].
Thank you in advance,
Will
how long will this swipeArray stay the same?
This is a bit too open ended. It’ll stay the same until you assign a new value to it, either from this same bit of code or a different part. Only you can know when that will be, by looking at your code.
Since you express an interest in keeping the code concise, here’s a couple of code tips.
You might think about writing your first snippet’s loop like this:
swipeArray = (0..<swipes.no_of_swipes).map { _ in
arc4random_uniform(84)
}
This combines creating a new array and populating the values. By the way, just in case you don’t realize, there’s no guarantee this array won’t contain the same value twice.
It’s also probably better to make swipeArray of type [Int] rather than [UInt32], and to convert the result of arc4random to an Int straight away:
Int(arc4random_uniform(84))
Otherwise the UInt32s will probably be a pain to work with.
For your second for loop, you can do this:
for i in swipeArray {
patternRoom.image = UIImage(named: "pattern\(i)")
// rest of code
}
When writing Swift, usually (but not always), when you find yourself using array[x] there’s a better more expressive way of doing it.

Actionscript 2 Proper way to define function on MovieClip

I'm trying to write a function on a MovieClip, and call it from the root clip. What works fine in ActionScript 3 doesn't seem to be working properly in ActionScript 2.
Frame 1 of the _root MovieClip:
var newMovieClip:MovieClip = _root.attachMovie('Notification', id, 0);
newMovieClip.SetNotificationText("Test text");
Frame 1 of the Notification MovieClip:
function SetNotificationText(inputText : String){
notificationText.text = inputText;
}
The result is that the MovieClip is created but the text is not changed.
Am I doing this wrong?
To add functions to a MovieClip in AS2, you need to use one of these methods:
Add the method to the prototype of MovieClip:
MovieClip.prototype.SetNotificationText = function(inputText:String):Void
{
if(this["notificationText"] !== undefined)
{
// If we're going to use the prototype, at least do some checks
// to make sure the caller MovieClip has the text field we expect.
this.notificationText.text = inputText;
}
}
newMovieClip.SetNotificationText("Test text");
Make the MovieClip and argument of the function:
function SetNotificationText(mc:MovieClip, inputText:String):Void
{
mc.notificationText.text = inputText;
}
SetNotificationText(newMovieClip, "Test text");
Add the method directly to the newly created MovieClip:
var newMovieClip:MovieClip = _root.attachMovie('Notification', id, 0);
newMovieClip.SetNotificationText(inputText:String):Void
{
notificationText.text = inputText;
}
newMovieClip.SetNotificationText("Test text");
Option 2 is best overall - it's the cleanest and avoids overhead of creating a new function for every new MovieClip. It also avoids messing around with the prototype, which at best should be used to add generic methods, like a removeItem() method on Array.

The supplied DisplayObject must be a child of the caller

yes i know this question gets asked all the time, but my inexperience has prevented me from finding the answer. I'm simply trying to remove an object from the screen if a button is clicked -
public function but1click(evtObj:MouseEvent)
{
trace("button one clicked");
if (gracestate == "grace")
{
removeChild(grace);
trace("grace removed");
}
}
I traced the parent of the object (grace) and it came back as null. This is driving me nuts, and help would be much appreciated!
The question is unclear though the most likely problem seems to be you're storing a value into grace that is being garbage collected/deleted.
If you create a variable inside a function but no reference to that variable exists outside the function it will be GC'd. Basically anything that isn't referred to by something else is deleted, the only object this isn't true for is the timeline. This stops the flash player lagging and avoids errors when the RAM becomes full. Thus:
function createGrace () {
var grace = new Object();
addChild(grace);
}
trace (grace); // returns null
var grace:Object;
function createGrace () {
grace = new Object();
addChild(grace);
}
trace (grace); // returns [Object]

How do you pass statements through an addEventListener?

Iv been trying to pass arguments through an addEventListener event in actionscript such as...
target.addEventListener("pComp", rakeSoil(target));
but i get errors.
Iv tried to google but no luck :/
Thanks for replying if you do :)
The target is already passed as part of the event, either event.currentTarget or event.target will be what you want.
If you want something else passed, create a custom event. Add the property to the custom event.
Try adding an additional method as your event listener:
target.addEventListener ("pComp", targetListener);
...
private function targetListener (event:Event):void {
rakeSoil (event.currentTarget);
}
How this is what you want:
{
var target:EventDispatcher = ...;
Function rakeSoil = function (e:Event):void
{
// handle target
}
target.addEventListener("pComp", rakeSoil);
}
rakeSoil is a first class function(or closure), when event is dispatched, it will be invoked, and you can access 'target' in it.
EDIT:
Have a look at Closure (computer science)
I have always found anonymous functions to be more trouble than they are worth. I would simply follow the standard event handler code layout. It's more formal and takes a little more effort up front, but there is no ambiguity and it is far more readable when you return to it a year from now (reduces head-scratching duration):
// Target extends EventDispatcher
private var target:Target;
public function listenToTarget();
{
target = new Target();
target.addEventListener("pComp", pCompHandler);
}
private function pCompHandler(event:Event):void
{
target.rakeSoil();
}
Although, now that I look at it more closely, why are you having this object do something that Target should be capable of handling internally on its own?

Resources