Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-23-2012, 12:28 PM   #1
fring
Registered User
 
Join Date: Aug 2012
Posts: 1
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
Old 08-26-2012, 02:42 AM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,514
Default

When you load a picture it is loaded and stored in a BitmapData instance and that BitmapData instance is added to a Bitmap instance. None of them can receive mouse events. But adding the bitmap to a Sprite for example does the trick since Sprite can receive mouse events.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Reply


Thread Tools
Display Modes 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. The time now is 04:08 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.