views:

31

answers:

1

http://soulseekrecords.org/psysci/animation/tarot.html

If you go to that page and click the deck of cards. Some clicks don't work. Most clicks do. This onclick problem also happens when testing the movie in flash.

//import tweening files
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import fl.controls.UIScrollBar;
import flash.display.Sprite;


//create array of image names
var imageNames:Array = new Array();
var descText:Array = new Array();

//variable for total number of images
var totalImages:uint = imageNames.length;

//create a variable to load the large image
var bigImageLoader:Loader = new Loader;
//add bigImageLoader to the display list

//create random number
var randomNumber:uint;

//Scroll Variables
var scrollBarAdd:Boolean = false;
var scrollBar:UIScrollBar = new UIScrollBar();

addChild(bigImageLoader);
bigImageLoader.x = 130;
bigImageLoader.y = 25;

//create a textField
var myText:TextField = new TextField();

//add text box to display list
addChild(myText);
myText.width = 400;
myText.height = 180;
myText.x = 20;
myText.y = 200;
myText.wordWrap = true;
myText.multiline = true;
myText.selectable = true;

var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0x000000;
format.size = 16;
format.underline = false;

myText.defaultTextFormat = format;

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("master.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event)
{
myXML = new XML (e.target.data);
for(var i:uint = 0; i < myXML.img.length() ; i++)
{
imageNames.push(myXML.img[i].text());
descText.push(myXML.txt[i].text()) ; 
}
totalImages = imageNames.length;
}
//add button
var deckOfCards:Deck = new Deck();

//add button to display list
addChild(deckOfCards);


//Set button x and y position
deckOfCards.x = 20;
deckOfCards.y = 20;

//set buttonmode
deckOfCards.buttonMode = true;

//make sure all clicks register with the thumb itself (not inner contents)
deckOfCards.mouseChildren = false;

//add click listener to the button
deckOfCards.addEventListener(MouseEvent.MOUSE_DOWN, buttonClickHandler);

function buttonClickHandler (event:MouseEvent)
{

for ( var i:uint = 0; i < totalImages; i++ )
{
randomNumber = Math.round(Math.random()*totalImages);

if (randomNumber == i)
{
//load the corect image into the big image loader
bigImageLoader.load(new URLRequest("images/" + imageNames[randomNumber] ) );
bigImageLoader.scaleX = .0;
TweenLite.to( bigImageLoader, .5, {scaleX:1 , ease:Expo.easeOut} );
myText.alpha = 0;
TweenLite.to( myText, .5, {alpha:1} );
myText.text = String (descText[randomNumber]);

if (myText.textHeight > myText.height)
{
scrollBar.scrollTarget = myText;  //assign the target of the scrollBar to your textfield
scrollBar.height = myText.height;  //make the height the same as the textfield
scrollBar.move(myText.x + myText.width, myText.y);  //Move the scrollbar to the righthand side
addChild(scrollBar);
scrollBar.update();
scrollBarAdd = true;
} else { 
if((myText.textHeight < myText.height) && scrollBarAdd == true){
scrollBarAdd = false;
removeChild(scrollBar);
}
}
}
}
}

my xml is structured like this

<?xml version="1.0" encoding="UTF-8"?>
<master>
<img>joker.png</img>
<img>aceOfClubs.png</img>
<img>twoOfClubs.png</img>
...


<txt>The average stage of man...</txt>
<txt>This is an Ace of Clubs.</txt>
<txt>This is a two of Clubs.</txt>
...
</master>
A: 

You may have done this already, but to discern if its the actually event or your code which is creating this problem, stick a trace in the buttonClickHandler before you do your loop.

I'm interested in if the event is actually firing when you click that deck...

Neil
It is firing after I added the trace. It traces a click but no card appears.
psy-sci
oh i just found something strange. If i trace(i); after "if (randomNumber == i)" within the for loop for the click handler strange things start to happen. Sometimes it returns three values for i and sometimes none at all. It seems I have found where the issue exists (with the code) but still unsure what I did wrong.
psy-sci
I fixed it! I moved the creation of the randomNumber before the for loop in the click handler. And now it works fine. It has some trouble loading certain cards. But I am sure this is because of poor optimization.Thank you Neil. I forget to trace things to figure out problems like this. Your reminder was very helpful. :)
psy-sci
Glad you fixed it, also can I suggest using int instead of uint in your for loops.
Neil