In the function below I try to throw random items (generated by the function generateItem() ) on the stage and let them move from outside the right side of the stage to just out of the left side. This works fine but the only problem is that ease:Linear.easeNone in the TweenLite.to function doesn't work. The items keep going fast in the beginning and slow at the end of the animation. Here is the code:
private function timerHandler(event:TimerEvent):void{
//item handling
if(gameTimer.currentCount % 4 === 0){
var item:MovieClip = generateItem();
arrItems.push(item);
for each(item in arrItems){
TweenLite.to(item,5,{x:-(item.width - 1), ease:Linear.easeNone});
trace(item + " ----- " + item.x);
if(item.x < -(item.width)){
arrItems.splice(arrItems.indexOf(item),1);
stage.removeChild(item);
}
}
}
What is going on here is that you are overriding your tweens again and again inside the for each loop. Once you have set up a tweening animation for an object with TweenLite.to you don't need to set it again for that same object, unless you want to override it with a new tween.
The reason you see the end of the animation going slower is because the distance the object has to move until the final point of the tweening animation is less (since it has already covered some of it), but the total duration of the animation is still 5 seconds. Speed = distance/time, so same time but shorter distance = slower tweening.
You can fix this by just moving the TweenLite.to call outside of the loop, so only one tween will be set up for each item. Another improvement I recommend for your code is to use the onComplete callback function of TweenLite to avoid that costly for each loop inside your timeHandler function, and remove the item only after the tweening has completed, unless you need to iterate through all items for some other reason than to just check if the tweening has ended.
For example:
private function timerHandler(event:TimerEvent):void
{
//item handling
if(gameTimer.currentCount % 4 === 0)
{
var item:MovieClip = generateItem();
arrItems.push(item);
//only call once for each item
TweenLite.to(item,5,{ x:-(item.width - 1),
ease:Linear.easeNone,
onComplete: removeItem,
onCompleteParams:[item]
});
}
}
private function removeItem(item:MovieClip):void
{
arrItems.splice(arrItems.indexOf(item),1);
stage.removeChild(item);
}
Hope this helps!
Related
What do I need to fix so that a single keystroke plays one full animation loop?
Creating animation object:
jojoStandSummonTextureAtlasRight = new TextureAtlas(Gdx.files.internal("jojo/stand_summon/right/jojo_stand_summon.atlas"));
jojoStandSummonAnimationRight = new Animation<TextureRegion>(1 / 3f, jojoStandSummonTextureAtlasRight.getRegions());
Rendering animations:
if(Gdx.input.isKeyJustPressed(E) && lastKeyInput == RIGHT){
sb.draw(jojo.jojoStandSummonAnimationRight.getKeyFrame(elapsedTime, false), jojo.getPosition().x, jojo.getPosition().y);
lastKeyInput = RIGHT;
}
The Animation class provided by LibGDX includes an .isAnimationFinished() function. This will return a boolean indicating when the animation has finished playing.
In your case, I assume you're updating your elapsedTime variable in your update() function. So you can do something like:
if(!jojoStandSummonAnimationRight.isAnimationFinished()) {
elapsed_time += Gdx.graphics.getDeltaTime();
} else {
// animation is finished
}
I've run across a piece of code that I'm really struggling to get working... It's meant to wait three seconds before fading an object (swslogo), however when I test it, it doesn't seem to work.. anyone know why this might be
var GameMode:Number = 0;
swslogo.alpha = .0;
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
intro();
function intro(e:Event=null):void
{
IntroTimer.reset()
IntroTimer.start();
}
function swsfadein(e:Event=null):void
{
IntroTimer.stop();
swslogo.addEventListener(Event.ENTER_FRAME, fadein)
}
function fadein(e:Event=null){
if(swslogo.alpha <=0){
this.alpha +=0.1;
if(swslogo.alpha >=1){
this.removeEventListener(Event.ENTER_FRAME, fadein);
}
}
}
Edit: Removed accidental line that wasn't meant to be there
From your last few questions I can tell you totally need to learn what a program is, in general, unrelated to AS3 of any other given language, instead of struggling with random pieces of code while treating them like magic spells.
// This part is fine.
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
// A method is called. It resets and restarts timer.
intro();
// Another method is called. It stops the times and starts the fading thing.
swsfadein ();
So basically that code in a single go:
Creates timer.
Starts timer.
Stops timer.
Starts fading.
Which obviously suppresses the intended use of the timer.
i am using AS3 to create a function that will automatically play a movieclip all the way through and then remove it. my project is going to have a lot of animated cutscenes, so id like to be able to call this function, use the cutscene id like as a parameter, and then move on to the next. the problem is, im trying to use the function multiple times in a row to play clips sequentially, but they're all playing at the same time. is there a fix, or a better way to do this altogether?
playClip(new a_walk); //find a way to make these stop playing at the same time
playClip(new a_door);
//a_walk and a_door are the AS linkage class names for the movieclips im referring to
function playClip (clip:MovieClip):void {
addChildAt(clip, 0);
clip.mask = myMask;
clip.x=412.4;
clip.y=244.5;
clip.addEventListener(Event.ENTER_FRAME, checkframes);
function checkframes(event:Event) {
if (clip.currentFrame == clip.totalFrames) {
//trace("wow! youre an idiot!");
if (clip.parent) {
clip.parent.removeChild(clip);
trace (100);
return;
}
}
}
}
Sounds like you want a mechanism to play a queue of MovieClips? If so, here is a way you can accomplish this:
//create an array with the clips you want to play (in order), in my example here, the items can be a MovieClip derived Class, or a MovieClip instance
var playQueue:Array = [a_walk, a_door];
//create a var to store the currently playing clip
var currentClip:MovieClip;
playNext(); //call this when you want the queue of clips to start playing
function playNext():void {
//if there was an item previously playing (currentClip has a value), stop it and remove it/dispose of it
if(currentClip){
currentClip.stop(); //stop it from playing
currentClip.addFrameScript(currentClip.totalFrames-1, null); //remove the frame script that was added
currentClip.parent.removeChild(currentClip); //remove it from the display
currentClip = null;
}
//check if there's anything left to play
if(playQueue.length < 1) return;
var nextItem:* = playQueue.shift(); //shift retrieves and removes the first item in the array;
if(nextItem is Class){
//if it's a class, instantiate it
currentClip = new nextItem();
}else{
currentClip = MovieClip(nextItem);
}
//initialize the movie clip
addChildAt(currentClip, 0);
currentClip.gotoAndPlay(1);
//this is just what you were doing before:
currentClip.mask = myMask;
currentClip.x=412.4;
currentClip.y=244.5;
//add a command on the last frame of the movie clip to play the next item in the queue
currentClip.addFrameScript(currentClip.totalFrames-1, playNext);
//addFrameScript is 0 based, so 0 would refer to the first frame. This is why we subtract 1 to get the last frame
}
I should note, that addFrameScript is an undocumented function. It serves as a nice shortcut so you don't have to have an ENTER_FRAME listener checking currentFrame vs. totalFrames. Being undocumented however, one can not count on it's continued existence in future versions of the Flash/AIR runtimes (though it's been around for a long long time)
note
This answer is a work in progress. I'm waiting on a response from the OP.
// playClip(new a_door); don't call this yet, or they will just both play.
var clipData:CustomClass = new CustomClass(); // add an instance of a custom class to hold the value of the movie
//clip being played (so that you can use this value later in the event handler.)
// it will also hold a value of the next clip
clipData._currentClip = a_walk;
clipData._nextClip = a_door;
playClip(new a_walk);
function playClip (clip:MovieClip):void {
addChildAt(clip, 0);
clip.mask = myMask;
clip.x=412.4;
clip.y=244.5;
clip.addEventListener(Event.ENTER_FRAME, checkframes);
}
function checkframes(event:Event) {
if (clipData._currentClip.currentFrame == clipData._currentClip.totalFrames) {
//trace("wow! youre an idiot!");
if (clipData._currentClip.parent) {
playClip(clipData._nextClip);
clipData._currentClip.parent.removeChild(clipData._currentClip);
clipData._currentClip = clipData._nextClip; // moves the clips down
//clipData._nextClip = here we have
//a problem. Do these clips play in a set
//order, first to last? Or do the play out of
//order jumping back and forth? If so, how
//are you planning on controlling which clip
//plays next?
trace (100);
return;
}
}
}
I haven't checked this in Flash yet to see if it works, but I noticed that you are defining a function inside another function, which I don't think is good practice, so this might clean things up for you. Give it a try and let us know.
I'll try to fix my code above when I get a chance. In the meantime, you answered my question about playing the clips in order, so a simple solution would be to put all the clips in an array and then play them by playClip(clipArray[i]) and then when the clip ends and gets removed, do i++ and call the same function playClip(clipArray[i]) which will play the next clip in the array.
My flash game exists of a timeline with multiple frames (I know I should avoid the timeline)
The point of the game is a point and click adventure. The object that you are able to pick up get spawned and destroyed accordingly as you enter and leave the room. now my problem is when entering frame 14 (accessibel from frame 12) it creates a piece of paper which you are able to pick up if you have another item. Now my problem is when you can't or don't pick up the paper and go back to frame 12 (only exit is to frame 12), you can't click on any other object and you are basicly stuck on frame 12. When leaving and entering other rooms it works properly but for some reason it doesn't for on the paper on frame 14.
My code to remove objects works as following
In my Main.as Documentclass I have a function that called as soon as the game starts which does the following
if (lastframe == 14)
{
trace (prop.numChildren);
while (prop.numChildren )
{
prop.removeChildAt(0);
}
}
The lastframe variable is established when moving from frames
this function is found on the frame itself (each exit function on it's own respective frame)
function exitKantine(event:MouseEvent):void
{
Main.lastframe = 14;
gotoAndStop(12);
}
The function to remove the prop actually removes it but then causes all other clickable objects to be unusable.
Thanks for looking at my question and thanks in advance for your suggestions
I would say instead of removing children, add it once in the beginning, add all the listeners in the beginning, and toggle the visibility instead of trying to addChild and removeChild every time you want to hide it. Use an array so you can have a few happening at the same time.
something like this:
private function init():void
{
assignVars();
addListeners();
stage.addChild // make sure this is in document class or you are passing stage to the class using it
}
for (var i = 0; i < _thingsAry.length; i++)
{
if (_thingsAry[i] == 14)
{
_thingsAry[i].visible = false;
trace("the visibility of _thingsAry[" + i + "] is " + _thingsAry[i].visible
}
}
I've tried everything. Arrays are quite simple so I don't know why this doesn't function:
var menuList:Array = [menu_bag_mc,menu_chips_mc,menu_coke_mc,menu_cup_mc,menu_deodorant_mc,menu_fork_mc,menu_knife_mc,menu_lighter_mc,menu_milk_mc,menu_pill_mc,menu_rings_mc,menu_shampoo_mc,menu_spoon_mc,menu_straw_mc,menu_toothbrush_mc,menu_trashbag_mc,menu_water_mc];
function captureAllClicks(event:MouseEvent):void
{
trace(menuList.indexOf(event.target));
}
stage.addEventListener(MouseEvent.CLICK, captureAllClicks);
Every time I click on any of the items on the stage (which are all given the instance names listed above. each is a tweening movieclip containing a button) I get a trace of -1. WHY?!
edit2
What needs to happen:
for each (var mc:MovieClip in menuList) mc.addEventListener(MouseEvent.CLICK, createContent);
//right here, i need to make a variable that I can put in the "addchild" so that
//for every one of the list items clicked, it adds a movieclip child with
//the same name (such as menu_bag_mc from above) with "_frame" appended.
var framevar:MovieClip = menuList[i] += "_frame";
function createContent(event:MouseEvent):void {
if(MovieClip(root).currentFrame == 850) {
while(MovieClip(root).numChildren > 1)
{
MovieClip(root).removeChild(MovieClip(root).getChildAt(MovieClip(root).numChildren - 1));
}
MovieClip(root).addChild (framevar);
MovieClip(root).addChild (closeBtn);
}
else {
MovieClip(root).addChild (framevar);
MovieClip(root).addChild (closeBtn);
MovieClip(root).gotoAndPlay(806);
}
}
If I can't do a variable, there's no point for the whole "for each" statement you put together... not really any point for an array, because I'll still have to create 20 lines of code for each separate one. What's the point of having an array if you can't make variables from them?
because quite obviously event.target seems not to be in menuList.
the most likely explanation is, that your MovieClips have children, that are being clicked on and thus are event.target.
You should probably set mouseChildren to false on all those MovieClips. Or you could register individual handlers per movieclip as this:
function captureAllClicks(event:MouseEvent):void {
trace(menuList.indexOf(event.currentTarget));
}
for each (var mc:MovieClip in menuList) mc.addEventListener(MouseEvent.CLICK, captureAllClicks);
greetz
back2dos
why don't you try event.currentTarget instead of event.target
Also to help yourself trouble shoot, why don't you just trace the event.target and trace the event.currentTarget. You can also loop through your array and trace all the objects in it. Then get a better visual idea of what is going on.