views:

326

answers:

4

Hi,

Making a flash page that can cycle through these three images on mouseclick. For some reason the local changes to count are not reflected on the global one. I tried _global but the syntax was odd and gave me errors. How should I implement this?

import flash.events.Event;

var images:Array = ["images/image.jpg", "images/image2.jpg", "images/image3.jpg"];
var count:int = 0;

forward.addEventListener(MouseEvent.CLICK, loadPhoto);


function loadPhoto(evt:Event){

    if(count>2){
        count = 0;
    }

    trace(count);
    imageFrame.source = images[count];

    count++;

}

A simplified version of the problem would be getting trace to output the number of times you've clicked.

import flash.events.Event;

var count:int = 0;

forward.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(evt:Event)
{
    trace(count);
    count++; 
}
+1  A: 

This should work fine. I'm assuming by the way the code is written, this is on the timeline and not in a class. It shouldn't make any difference -

However try referencing the "count" variable inside your function with the this

It may look something more like:

function loadPhoto(evt:Event){

    if(this.count>2){
        this.count = 0;
    }

    trace(this.count);
    imageFrame.source = images[this.count];

    this.count++;

}

It's more verbose and pedantic, but your initial code should work just fine. I might add - this code doesn't use the 'count' variable outside the function, alternative to declaring it - is the problem, the counter is always '0' when the function is run?

Glycerine
yes that is the current problem
msandbot
I am trying to keep track of where I am in the array. There will be buttons that go forward 1 and back 1. They need to know which image is currently being shown. Right now and with the this.count added in count is always zero.
msandbot
+3  A: 

I think your issue is a scope one. Try this instead:

import flash.events.Event;

var images:Array = ["images/image.jpg", "images/image2.jpg", "images/image3.jpg"];
var count:int = 0;

forward.addEventListener(MouseEvent.CLICK, clickHandler);


function clickHandler(evt:Event)
{
    loadPhoto(); // Notice, this is calling a function already defined on root!
}

function loadPhoto()
{
    trace(count);
    // Use modulous to deal with this type of behavior -- it is easier in the end.
    imageFrame.source = images[count%images.length];

    count++; // Count should be within scope here.
}
Christopher W. Allen-Poole
it is still always zero, I do like the modulus thoughThanks
msandbot
+1 for the good oop programming.
Glycerine
+1  A: 

Any chance that the code is in a frame that is executing multiple times? (because you missed a stop() somewhere, for example)

If so, you might have a problem you hadn't noticed which is causing this strange behaviour. A simple way to check if this is happening is adding a trace("test") after declaring count (or before, but put it in that frame script).

Juan Pablo Califano
probably the problem, let me check
msandbot
how do i not put it in a frame? sorry I'm new to flash
msandbot
One way is defining a class. Put your code there and link your Movieclip in the library to that class.
Juan Pablo Califano
so i got rid of the = 0 on var cound:int = 0 and now it works in the frame loop. But I have a feeling I should change this. Thanks
msandbot
No problem. The frame loop could be a problem if you have code that is meant to be executed just once. You can move your code to an external class and "attach" that code to your movieclip. There are many tutorials for this online. One that came up googling: http://www.flashconf.com/actionscript/actionscript-3-external-classes/
Juan Pablo Califano
Other than that, you might get away with a boolean flag (it's kind of hacky, but would work). `var defined:Boolean; if(!defined) { defined = true; // put the code that have to run just once here }`.
Juan Pablo Califano
A: 

How do you program it so it counts in 2s? (ie, 0, 2, 4, 6, etc)?

Jimmy