Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-23-2012, 01:51 PM   PM User | #1
fring
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
fring is an unknown quantity at this point
Smile How to add click event to bitmap ?

hey guys, i have this photo gallery that is linked to an xml file, and was wondering how do you add a click event to the image? for example when you click on first image, or the other images to open them in a popup window.

here is the actionscript txt:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;

var galleryXML:XML;
var xmlLoader:URLLoader;
var xmlRequest:URLRequest;

xmlLoader = new URLLoader();
xmlRequest = new URLRequest('galleryXML.xml');

var numOfImages:int;
var currImage:int = 0;

var imageLoader:Loader;
var imageRequest:URLRequest;

imageLoader = new Loader();

var imagesList:Array = new Array();
var loadedImages:Array = new Array();

xmlLoader.load(xmlRequest);
xmlLoader.addEventListener(Event.COMPLETE,onComple te);

function onComplete(e:Event):void
{
galleryXML = new XML(xmlLoader.data);
numOfImages = galleryXML.photo.length();
var i:int = numOfImages;
while(i--)
{
imagesList.push(String(galleryXML.photo[i].url));
}
startLoading();
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE,loadNext);
}

function startLoading():void{
imageRequest = new URLRequest(imagesList[currImage]);
imageLoader.load(imageRequest);
}

function loadNext(e:Event):void
{
if(currImage == numOfImages-1)
{
loadedImages.push(imageLoader.content);
imageLoader.contentLoaderInfo.removeEventListener

(Event.COMPLETE,loadNext);

var i:int = galleryXML.photo.length();
container = new Sprite();
while(i--)
{
var rect:Bitmap = loadedImages[i];
var bitmapCont:Sprite = new Sprite();
bitmapCont.addChild(rect);
container.addChild(bitmapCont);
bitmapCont.x = i * 120;
bitmapCont.width = 100;
bitmapCont.height = 60;
bitmapCont.addEventListener(MouseEvent.CLICK,draw) ;
bitmapCont.addEventListener(MouseEvent.MOUSE_OVER, overFunc);
bitmapCont.addEventListener(MouseEvent.MOUSE_OUT,o utFunc);
}
addChild(container);
container.y = 325;
addEventListener(Event.ENTER_FRAME,update);

return;
}
loadedImages.push(imageLoader.content);
currImage++;
imageRequest = new URLRequest(imagesList[currImage]);
imageLoader.load(imageRequest);
}

function overFunc(e:Event):void
{
e.currentTarget.width = 110;
e.currentTarget.height = 70;
}

function outFunc(e:Event):void
{
e.currentTarget.width = 100;
e.currentTarget.height = 60;
}


import flash.display.Sprite;
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;

var bd:BitmapData = new BitmapData(1022,767);

var b:Bitmap = new Bitmap(bd);
b.x = 75;
b.y = 15;
b.width = 400;
b.height = 300;
addChild(b);
var container:Sprite

var vel:Number = 0;
var acc:Number = 0;
var damp:Number = 0.95;


function update(e:Event):void
{
acc = 0;
if(mouseX<150)
acc = - - - -0.5;
if(mouseX>400)
acc = - - -0.5;
vel +=acc;
vel*=damp;
container.x += vel;
if(container.x>0)
container.x = 0;
if(container.x < 550-container.width)
container.x = 550-container.width;
}

function draw(e:MouseEvent):void
{
bd.draw(Sprite(e.currentTarget));
}

here is the xml one:

<?xml version="1.0" encoding="utf-8" ?>
<gallery>


<photo>
<url> 1.jpg </url>
</photo>

<photo>
<url> 2.jpg </url>
</photo>

<photo>
<url> 3.jpg </url>
</photo>

</gallery>

the fla file ,pictures and xml file must be in the same folder. have a go at it to see how it is, maybe you can do it.
thanks!
fring is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:25 PM.


Advertisement
Log in to turn off these ads.