I would like to generate some random words when an object hits another one.
I've tried it but only one word comes up and never changes again.
Also would it be better if I make a new class for the random texts or is including it in the Main class good enough? Thanks!
Here's my current code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends MovieClip {
public var cell:Cell;
public var group:Array;
public var gameTimer:Timer;
public var score:int = 0;
var array:Array = new Array ("Apples",
"Bananas",
"Grapes",
"Oranges",
"Pineapples"); //create an array of possible strings
var randomIndex:int = Math.random () * array.length;
//public var randomTxt:String;
public function Main() {
group = new Array();
gameTimer = new Timer (25);
gameTimer.addEventListener(TimerEvent.TIMER,moveCell);
gameTimer.start();
}
private function createCell(_x:Number = 0, _y:Number = 0):void {
var cell:Cell = new Cell(_x, _y);
group.push(cell);
addChild(cell);
}
function moveCell (timerEvent:TimerEvent):void {
if (Math.random() < 0.01) {
var randomX:Number = Math.random()*800;
var randomY:Number = Math.random()*600;
createCell(randomX, randomY);
}
for (var i:int = 0; i < group.length; i++)
{
var cell:Cell = Cell(group[i]);
if (cell.hitTestObject(island))
{
cell.parent.removeChild(cell);
group.splice(i,1);
score++;
txtWordDisplay.text = "Killed by" + array [randomIndex];
}
}
scoreOutPut.text = score.toString();
}
}
}
It's always the same word because you only set the randomIndex once at the beginning of your program.
A simple fix would be to update that value when the collision happens:
if (cell.hitTestObject(island))
{
...
randomIndex = Math.floor(Math.random () * array.length);
txtWordDisplay.text = "Killed by" + array [randomIndex];
}
Related
Making a map creation program. Field.as instantiates _player. Player.as addsChild _editorPanel to parent. EditorPanel.as instantiates a movieclip called tilepalette when I click a button in the editor panel. Tilepalette holds all of my tilesets that I want to be able to choose a tile from by clicking the tile that I want.
Field.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.FrameLabel;
import flash.display.DisplayObjectContainer;
import flash.geom.Rectangle;
public class Field extends MovieClip{
private var player:Player;
private var sampleTile:Tile = new Tile();
private var _tilePalette:TilePalette;
private var _paletteArray:Array = [];
public function Field()
{
player = new Player();
player.x = 0;
player.y = 0;
addChild(player);
GetSampleTiles();
}
private function GetSampleTiles()
{
for (var i:int = 1; i <= sampleTile.totalFrames; i++)
{
var tileObj:Object = new Object();
sampleTile.gotoAndStop(i);
var graphicData:BitmapData = new BitmapData(32,32);
graphicData.draw(sampleTile);
tileObj.Name = sampleTile.currentFrameLabel;
tileObj.Graphic = graphicData;
tileObj.Frame = sampleTile.currentFrame;
Engine._tilesData.push(tileObj);
}
}
}
}
Player.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Player extends MovieClip
{
private var _inp:Input = new Input();
private var _playerXindex:int = 0;
private var _playerYindex:int = 0;
private var _heldTile:Object;
private var _editorPanel:EditorPanel;
public function Player()
{
addChild(_inp);
addEventListener(Event.ENTER_FRAME, HandleKeys);
_heldTile = new Object();
}
private function HandleKeys(e:Event)
{
_playerXindex = x/32;
_playerYindex = y/32;
if(_inp.keyUp)
{
y -= 32;
}
if(_inp.keyDown)
{
y += 32;
}
if(_inp.keyLeft)
{
x -= 32;
}
if(_inp.keyRight)
{
x += 32;
}
if(_inp.keySpace)
{
try{DrawATile(_heldTile);}
catch(err:Error){trace("Bad or no graphic. Using this instead.");}
finally{DrawATile(Engine._tilesData[0]);}
}
if(_inp.keyA)
{
trace(Engine.tileObjIndex[_playerYindex][_playerXindex].Name);
}
if(_inp.keyB)
{
BuildStarterIndex();
SetHeldTile(Engine._tilesData[0]);
}
}
private function DrawATile(tileToDraw:Object)
{
if (Engine.tileObjIndex[_playerYindex][_playerXindex].Name.indexOf("Placeholder") >= 0)
{
trace("set");
var graphicData:BitmapData = new BitmapData(32,32);
graphicData.draw(tileToDraw.Graphic);
Engine.tileObjIndex[_playerYindex][_playerXindex].Name = tileToDraw.Name;
Engine.tileObjIndex[_playerYindex][_playerXindex].Graphic = tileToDraw.Graphic;
var newTile:Bitmap = new Bitmap(tileToDraw.Graphic);
newTile.x = x;
newTile.y = y;
parent.addChild(newTile);
}
}
private function BuildStarterIndex()
{
_editorPanel = new EditorPanel();
_editorPanel.x = 1;
_editorPanel.y = 544;
parent.addChild(_editorPanel);
for (var i:int = 0; i < 20; i++)
{
Engine.tileObjIndex[i] = [];
for (var u:int = 0; u < 20; u++)
{
var tileObj:Object = new Object();
var graphicData:BitmapData = new BitmapData(32,32);
graphicData.draw(Engine._tilesData[0].Graphic);
tileObj.Name = "Placeholder" + "["+i+"]"+"["+u+"]";
tileObj.Graphic = graphicData;
tileObj.Frame = 0;
Engine.tileObjIndex[i].push(tileObj);
}
}
}
private function SetHeldTile(tiletoset:Object)
{
_heldTile.Name = tiletoset.Name;
_heldTile.Graphic = tiletoset.Graphic;
_heldTile.Frame = tiletoset.Frame;
_editorPanel.tileName.text = _heldTile.Name;
_editorPanel.tileFrame.text = _heldTile.Frame;
var heldTile:Bitmap = new Bitmap(_heldTile.Graphic);
heldTile.x = 30;
heldTile.y = 31;
_editorPanel.addChild(heldTile);
}
}
}
EditorPanel.as:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Bitmap;
import flash.events.Event;
public class EditorPanel extends MovieClip
{
public var _tilePalette:TilePalette;
private var _pArray:Array = [];
private var _mouseXi:int;
private var _mouseYi:int;
public var _tileGrabbed:Object;
public function EditorPanel()
{
btnpalette.addEventListener(MouseEvent.CLICK, BuildPalette);
}
private function UpdateMouse(e:Event)
{
_mouseXi = Math.floor(_tilePalette.mouseX/32);
_mouseYi = Math.floor(_tilePalette.mouseY/32);
}
private function BuildPalette(e:MouseEvent)
{
_tilePalette = new TilePalette();
_tilePalette.x = mouseX;
_tilePalette.y = mouseY-256;
addChild(_tilePalette);
var counter:int = 1;
for (var i:int = 0; i < 8; i++)
{
_pArray[i] = [];
for(var u:int = 0; u < 8; u++)
{
if(counter >= Engine._tilesData.length)
{
counter = 1;
}
var b:Bitmap = new Bitmap(Engine._tilesData[counter].Graphic);
b.x = 32 * u;
b.y = 32 * i;
_tilePalette.addChild(b);
_pArray[i].push(Engine._tilesData[counter]);
counter++;
}
}
_tilePalette.addEventListener(MouseEvent.MOUSE_MOVE, UpdateMouse);
_tilePalette.addEventListener(MouseEvent.CLICK, GrabATile);
}
private function GrabATile(e:MouseEvent)
{
return _pArray[_mouseXi][_mouseYi];
}
}
}
The problem is that when I do GrabATile, I don't know what to do with the returned Object reference. I want to use that Object in the Player function SetHeldTile.
So this will make it work, but I would suggest looking to refactor that code a bit. Adding children that add things to their parents is not the best design... There is also round robin stuff going on there where the editor selects the tile, but then the player sets things like tileName back on the editor after it is told about the new tile. This should occur in the editor where tile selection occurs etc.
That said, depending on your flow, here is how to make it work:
EditorPanel already has a property _tileGrabbed. so in your GrabATile() method just set
_tileGrabbed = _pArray[_mouseXi][_mouseYi];
Then in your setHeldTile() method... simply say
_heldTile = _editorPanel._tileGrabbed.
Now, if you need GrabATile() to TRIGGER setHeldTile(), the best way to handle that would be with an event.
// in EditorPanel
private function GrabATile(e:MouseEvent){
_tileGrabbed = _pArray[_mouseXi][_mouseYi];
dispatchEvent(new Event("tileChanged"));
}
// in Player
private function BuildStarterIndex(){
_editorPanel = new EditorPanel();
_editorPanel.addEventListener("tileChanged", setHeldTile);
// ... rest of code
}
private function SetHeldTile(event:Event){
_heldTile = _editorPanel._tileGrabbed;
// ... rest of code
}
hello how to put sounds to this code so when tower firing bullet sound will show up together with it...i'm just nubie so i really need a help
can you help me how to put sounds in this code
this is the code
package Game
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import Gamess.BulletThree;
import Games.BulletTwo;
public class Tower_Fire extends MovieClip
{
private var isActive:Boolean;
private var range,cooldown,damage:Number;
public function Tower_Fire()
{
isActive = false;
range = C.TOWER_FIRE_RANGE;
cooldown = C.TOWER_FIRE_COOLDOWN;
damage = C.TOWER_FIRE_DAMAGE;
this.mouseEnabled = false;
}
public function setActive()
{
isActive = true;
}
public function update()
{
if (isActive)
{
var monsters = GameController(root).monsters;
if (cooldown <= 0)
{
for (var j = 0; j < monsters.length; j++)
{
var currMonster = monsters[j];
if ((Math.pow((currMonster.x - this.x),2)
+ Math.pow((currMonster.y - this.y),2)) < this.range)
{
//spawn new bullet
var bulletRotation = (180/Math.PI)*
Math.atan2((currMonster.y - this.y),
(currMonster.x - this.x));
var newBullet = new Bullet(currMonster.x,currMonster.y,
"fire",currMonster,this.damage,bulletRotation);
newBullet.x = this.x;
newBullet.y = this.y;
GameController(root).bullets.push(newBullet);
GameController(root).mcGameStage.addChild(newBullet);
this.cooldown = C.TOWER_FIRE_COOLDOWN;
break;
}
}
}
else
{
this.cooldown -= 1;
}
}
}
}
}
Firstly, you need to export sound for AS:
and then you can play sound from code:
var sound:Sound = new Track();
sound.play();
UPDATE:
if ((Math.pow((currMonster.x - this.x),2) + Math.pow((currMonster.y - this.y),2)) < this.range)
{
//spawn new bullet
var sound:Sound = new Track();
sound.play();
var bulletRotation = (180/Math.PI)*
Math.atan2((currMonster.y - this.y), (currMonster.x - this.x));
...
Hi I am pretty new to AS3, i am trying to parse some data from php to a VO file, and then transfer the string of data into another .as file where it will put the data into boxes. I am stuck in how do i parse the data from the VO file into the other .as File(Pulling the data from php into BookVO, then parsing BookVO to VectorTest). I tried tracing the data in BookVO, it works ok, but i can't get the data from BookVO to VectorTest.
Please help, thanks
BookVO.as
package com.clark
{
import flash.display.*;
import flash.net.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
public class BookVO
{
public var nobed1:String;
public var LoZip1:String;
public var rangelow1:String;
public var rangehigh1:String;
public var Bend:URLRequest;
public var variabless:URLVariables;
public var nLoader:URLLoader;
public function BookVO() {
Bend = new URLRequest("http://localhost/Autoresult.php");
Bend.method = URLRequestMethod.POST;
variabless = new URLVariables();
Bend.data = variabless;
nLoader = new URLLoader();
nLoader.dataFormat = URLLoaderDataFormat.TEXT;
nLoader.addEventListener(Event.COMPLETE,Jandler);
nLoader.load(Bend);
// handler for the PHP script completion and return of status
function Jandler(event:Event) {
var responseVariables: URLVariables = new URLVariables(event.target.data);
this.nobed1 = responseVariables.nobed1 ;
this.LoZip1 = responseVariables.LoZip1;
this.rangelow1 = responseVariables.rangelow1;
this.rangehigh1 = responseVariables.rangehigh1;
}
}
}
}
VectorTest.as
package com.clark
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
public class VectorTest extends MovieClip
{
public function VectorTest()
{
super();
var books:Vector.<BookVO> = new Vector.<BookVO>();
for (var i:int = 0; i < length; i++)
{
var book:BookVO = new BookVO();
book.nobed1 = "nobed1";
book.LoZip1 ="LoZip1";
book.rangelow1 = "rangelow1";
book.rangehigh1 ="rangehigh1";
books.push(book);
}
for (var j:int = 0; j < books.length; j++)
{
trace("Test", j, "has a name of", books[j].nobed1);
trace("Test", j, "Zip", books[j].LoZip1);
trace("Test", j, "ranglow", books[j].rangelow1);
trace("Test", j, "rangehigh", books[j].rangehigh1);
books[j].nobed1;
books[j].LoZip1;
books[j].rangelow1;
books[j].rangehigh1;
}
var currentY:int = 270;
for (var k:int = 0; k < books.length; k++)
{
var Bolder:Listing2 = new Listing2();
Bolder.x=80;
var tf:TextField = new TextField();
var tf1:TextField = new TextField();
tf1.width = 100;
var tf2:TextField = new TextField();
tf2.width = 100;
tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
tf.width = 100;
tf.autoSize = TextFieldAutoSize.CENTER;
tf1.width = 100;
tf1.autoSize = TextFieldAutoSize.CENTER;
tf2.autoSize = TextFieldAutoSize.CENTER;
tf2.width = 100;
tf1.y= tf.height+5;
// Pulling the textfields content out from the current bookVO
tf2.text = books[k].nobed1;
tf1.text = books[k].rangelow1;
tf.text = books[k].rangehigh1;
tf1.x = (Bolder.height-tf.height)*.5
tf2.x = (Bolder.height-tf.height)*.5
tf.x = (Bolder.height-tf.height)*.5
tf.y = (Bolder.height-tf.height)*.15
Bolder.addChild(tf);
Bolder.addChild(tf1);
Bolder.addChild(tf2);
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
}
First off, good job on your classes. Much better than most people who say they're new to AS3. ;)
That said...
In VectorTest, you're calling super() on a MovieClip. You don't need this.
If you're not using the timeline of a MovieClip (which, I'd recommend not using anyway), extend Sprite instead; it's lightweight as it doesn't carry the timeline baggage MovieClip does.
Your first for loop in VectorTest Constructor loops to length... of no array.
You're overwriting the properties of BookVO in that loop, which should be populated by your URLLoader. This is superfluous.
Your second loop references the 4 properties of BookVO, neither reporting or setting the variables (ie., books[j].nobed1;). Might want to remove that.
In BookVO, you've written a nested function. Unless you're dealing with a massive number of variables and have issues with variable scope, don't do it. As it stands, the only variables Jandler() is accessing are already Class level globals.
Loading data is an asynchronous operation, meaning, data doesn't populate instantly (LoadRequest > Wait > ProgressEvent > Wait > LoadComplete). Because you're both instantiating BookVO and reading the properties in the same function, all you'll get are null properties. Unlike the VectorTest constructor, because Jandler() is called on Event.COMPLETE (asynchronously), it will have access to the variables you're looking for.
Try this instead...
You'll still need to address the length of how many books you want to instantiate, however, I've split out your reading of the properties from the constructor, and added a reference to the method to call when the loading is complete.
It will print out all the variables, and if it is the last book in your Vector, it'll call finish() which... um... does the rest of what you were doing. :)
-Cheers
BookVO Updated 2013.11.07 # 12:30 AM
package com.clark{
import flash.display.*;
import flash.net.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
public class BookVO {
public var nobed1:String;
public var LoZip1:String;
public var rangelow1:String;
public var rangehigh1:String;
public var Bend:URLRequest;
public var variabless:URLVariables;
public var nLoader:URLLoader;
private var callMethod:Function;
public var data:Object;
public function BookVO(listener:Function = null) {
Bend = new URLRequest("http://localhost/Autoresult.php");
Bend.method = URLRequestMethod.POST;
variabless = new URLVariables();
Bend.data = variabless;
nLoader = new URLLoader();
nLoader.dataFormat = URLLoaderDataFormat.TEXT;
nLoader.addEventListener(Event.COMPLETE,Jandler);
nLoader.load(Bend);
if (listener != null) {
callMethod = listener;
}
}
public function Jandler(event:Event) {
// handler for the PHP script completion and return of status
var responseVariables:URLVariables = new URLVariables(event.target.data);
data = event.target.data;
report(data);
if (callMethod != null) {
callMethod(this);
}
}
private function report(obj:*, prefix:String = ""):void {
for (var k in obj) {
var type:String = getType(obj[k]);
if (type == "Array" || type == "Object" || type == "Vector") {
trace(prefix + k + ": (" + type + ") ¬")
report(obj[k], prefix + " ")
} else {
trace(prefix + k + ":" + obj[k] + " (" + type + ")")
}
}
}
private function getType(value:*):String {
// Returns the class name of object passed to it.
var msg:String = flash.utils.getQualifiedClassName(value);
if (msg.lastIndexOf("::") != -1) {msg = msg.split("::")[1];}
return msg;
}
}
}
VectorTest
package com.clark {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Sprite;
public class VectorTest extends MovieClip {
public var books:Vector.<BookVO>;
public function VectorTest() {
books = new Vector.<BookVO>();
for (var i:int = 0; i < length; i++) {
var book:BookVO = new BookVO(response);
books.push(book);
}
}
private function response(book:BookVO):void {
trace("Name: ", book.nobed1);
trace("Zip: ", book.LoZip1);
trace("ranglow: ", book.rangelow1);
trace("rangehigh: ", book.rangehigh1);
// call finish() if this is the last book.
if (books.indexOf(book) == books.length - 1) {
finish();
}
}
private function finish():void {
var currentY:int = 270;
for (var k:int = 0; k < books.length; k++) {
var Bolder:Listing2 = new Listing2();
Bolder.x=80;
var tf:TextField = new TextField();
var tf1:TextField = new TextField();
tf1.width = 100;
var tf2:TextField = new TextField();
tf2.width = 100;
tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
tf.width = 100;
tf.autoSize = TextFieldAutoSize.CENTER;
tf1.width = 100;
tf1.autoSize = TextFieldAutoSize.CENTER;
tf2.autoSize = TextFieldAutoSize.CENTER;
tf2.width = 100;
tf1.y = tf.height+5;
// Pulling the textfields content out from the current bookVO
tf2.text = books[k].nobed1;
tf1.text = books[k].rangelow1;
tf.text = books[k].rangehigh1;
tf1.x = (Bolder.height-tf.height)*.5
tf2.x = (Bolder.height-tf.height)*.5
tf.x = (Bolder.height-tf.height)*.5
tf.y = (Bolder.height-tf.height)*.15
Bolder.addChild(tf);
Bolder.addChild(tf1);
Bolder.addChild(tf2);
// position the object based on the accumulating variable.
Bolder.y = currentY;
addChild(Bolder);
currentY += Bolder.height + 35;
}
}
}
}
I'm trying to figure out how to reference the class of a target. Here is some of the code:
xmlDoc = new XML(xmlLoader.data);
//trace(xmlDoc.Video[1].Desc);
for (var i:int = 0; i < xmlDoc.Video.length(); i++)
{
xmlObj = new FilmVideo(xmlDoc.Video[i].Name, xmlDoc.Video[i].title, xmlDoc.Video[i].Thumb, xmlDoc.Video[i].URL, xmlDoc.Video[i].APILoader);
XMLItem[i] = xmlObj;
//trace(XMLItem);
MovieClip(root).main_mc.thumb_mc.addChild(XMLItem[i]);
if (i <= 0) {
XMLItem[i].x = 20;
XMLItem[i].y = 0;
} else if (i > 0){
XMLItem[i].x = XMLItem[i-1].x + XMLItem[i-1].width + 120;
XMLItem[i].y = 0;
}
XMLItem[i].addEventListener(MouseEvent.CLICK, makeThumbClick);
XMLItem[i].addEventListener(MouseEvent.MOUSE_OVER, makeThumbRollOver);
XMLItem[i].addEventListener(MouseEvent.ROLL_OUT, makeThumbRollOut);
}
}
function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
trace(FilmVideo(e.target));
/MovieClip(root).main_mc.theater_mc.videoLoader(FilmVideo(e.target)._APILoad, FilmVideo(e.target)._videoURL);
}
The XMLItem is an array that's storing a class object I custom made (the class name is FilmVideo based off movieclip). The _thumbToMC is a method within my custom class that returns a movieclip. The class has info stored within its properties I would like to pass through a function called in the makeThumbClick function. However, I have no idea how. e.target reference the _thumbToMC movieclip rather than the class. I do I reference the class? Thank you in advance :)
Here is the class:
package filmvideo
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.net.URLRequest;
public class FilmVideo extends MovieClip
{
public var _nameXML:String = "";
public var _title:String = "";
public var _thumbURL:URLRequest;
public var _videoURL:URLRequest;
public var _APILoad:String = "";
public var loader:Loader = new Loader();
public function FilmVideo(name:String, title:String, thumbURL:String, videoURL:String, APILoad:String)
{
_nameXML = name;
_title = title;
_thumbURL = new URLRequest(thumbURL);
_videoURL = new URLRequest(videoURL);
_APILoad = APILoad;
//trace(_name);
//trace(_title);
//trace(thumbURL);
//trace(videoURL);
//trace(_APILoad);
this.addChild(loader);
loader.load(_thumbURL);
}
}
}
You could simplify your class to:
package filmvideo
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.net.URLRequest;
public class FilmVideo extends MovieClip
{
public var _nameXML:String = "";
public var _title:String = "";
public var _thumbURL:URLRequest;
public var _videoURL:URLRequest;
public var _APILoad:String = "";
public var loader:Loader = new Loader();
public function FilmVideo(name:String, title:String, thumbURL:String, videoURL:String, APILoad:String)
{
_nameXML = name;
_title = title;
_thumbURL = new URLRequest(thumbURL);
_videoURL = new URLRequest(videoURL);
_APILoad = APILoad;
//trace(_name);
//trace(_title);
//trace(thumbURL);
//trace(videoURL);
//trace(_APILoad);
this.addChild(loader);
loader.load(_thumbURL);
}
}
}
Then you can use FilmVideo as a MovieClip (since it extends the MovieClip class).
And use 'currentTarget' instead of 'target', because it always points to the listened object, while 'target' points to the object which fired the event. more info here
xmlDoc = new XML(xmlLoader.data);
//trace(xmlDoc.Video[1].Desc);
for (var i:int = 0; i < xmlDoc.Video.length(); i++)
{
xmlObj = new FilmVideo(xmlDoc.Video[i].Name, xmlDoc.Video[i].title, xmlDoc.Video[i].Thumb, xmlDoc.Video[i].URL, xmlDoc.Video[i].APILoader);
XMLItem[i] = xmlObj;
//trace(XMLItem);
Object(root).main_mc.thumb_mc.addChild(XMLItem[i]);
if (i <= 0) {
XMLItem[i].x = 20;
XMLItem[i].y = 0;
} else if (i > 0){
XMLItem[i].x = XMLItem[i-1].x + XMLItem[i-1].width + 120;
trace(XMLItem[i].width);
XMLItem[i].y = 0;
}
XMLItem[i].addEventListener(MouseEvent.CLICK, makeThumbClick);
XMLItem[i].addEventListener(MouseEvent.MOUSE_OVER, makeThumbRollOver);
XMLItem[i].addEventListener(MouseEvent.ROLL_OUT, makeThumbRollOut);
}
function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
trace(myFilmVideo._APILoad);
MovieClip(root).main_mc.theater_mc.videoLoader(FilmVideo(e.currentTarget)._APILoad, FilmVideo(e.currentTarget)._videoURL);
}
If you don't want to do this way
Add a reference (inside the class) to the _thumbToMC back to it's FilmVideo object.
_thumbMC._filmVideo = this;
Then:
function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
MovieClip(root).main_mc.theater_mc.videoLoader(MovieClip(e.currentTarget)._filmVideo._APILoad, MovieClip(e.currentTarget)._filmVideo._videoURL);
}
I'm not 100% sure I understand the question, but assuming you want to retrieve the properties of the FilmVideo instance (which it appears the user is clicking on) then maybe this is what you are looking for;
function makeThumbClick(e:MouseEvent){
var myFilmVideo:FilmVideo = FilmVideo(e.target);
// access properties of _thumbToMC
myFilmVideo.randomproperty = 123;
}
I need to make a calendar in AS3 that looks like this regularly:
and this when a date is clicked:
I have the basics down, I think but I don't know where to go from here and I cannot figure out what is wrong with my code to make the days work properly.
Main Code:
package code {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
private var days:Array = new Array();
public var selectedDay:Day = null;
public function Main() {
// constructor code
var across: int = 7;
for( var i:int = 0; i < 31; i++)
{
var row:int = Math.floor( i / across );
var col:int = i % across;
var d:Day = new Day();
addChild(d);
d.x = col * (d.width);
d.y = row * (d.height);
days.push(d);
d.addEventListener(MouseEvent.CLICK, onClick);
}
}
public function onClick(e:MouseEvent):void{
if (selectedDay == null){
trace("meow!");
days[1].gotoAndStop(2);
}
else if (selectedDay != null){
}
}
}
}
and the Day code:
package code {
import flash.display.MovieClip;
import flash.text.TextField;
public class Day extends MovieClip {
public var weekday_txt:TextField;
public var date_txt:TextField;
public function Day() {
// constructor code
for (var num:int = 0; num < 7; num++){
var weekDays:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
weekday_txt.text = weekDays[num];
//trace(weekDays[num]);
date_txt.text = ""+42;
}
}
}
}
Thanks for any help!
I rewrote your classes as it is easier than trying to explain the logical errors:
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip{
private var days:Array = new Array();
private var selectedDay:Day;
public function Main() {
stop();
days = new Array();
var weekDayTitles:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var across:int = 7;
for (var i:int=0; i<31; i++){
var row:int=Math.floor(i/across);
var col:int = i%7;
var d:Day = new Day();
addChild(d);
d.x = col * (d.width);
d.y = row * (d.height);
d.setWeekDay(weekDayTitles[col]);
d.setDate(""+(i+1));
days.push(d);
d.addEventListener(MouseEvent.CLICK, onClick);
}
}
public function onClick(e:MouseEvent):void{
if (selectedDay != null){
selectedDay.setAsUnSelected();
}
selectedDay = Day(e.target);
selectedDay.setAsSelected();
}
}// Class
And your Day class should look like this:
// make sure this class is linked to a movieclip
// in your library that has two frames
// frame 1 = "unselected"
// frame 2 = "selected";
// make sure to have 2 textfields in the linked symbol, one named : "weekdayTxt" and the other "dateTxt"
public class Day extends MovieClip{
public var weekday_txt:TextField;
public var date_txt:TextField;
public function Day() {
gotoAndStop(1);
weekday_txt = this.weekdayTxt;
date_txt = this.dateTxt;
}
public function setWeekDay(_day:String):void{
weekday_txt.text = _day;
}
public function setDate(_date:String):void{
day_txt.text = _date;
}
public function setAsSelected():void{
this.gotoAndStop(2);
}
public function setAsUnSelected():void{
this.gotoAndStop(1);
}
}// Class
Well, I think you do have the jist of it, but I notice that your onClick method is always targetting the second day of the array and setting it to goto frame 2, since it doesn't look like you ever initialize "selectedDay"...:
public function onClick(e:MouseEvent):void{
if (selectedDay == null){
trace("meow!");
days[1].gotoAndStop(2);
}
else if (selectedDay != null){
}
}
it might help to change the function to:
public function onClick(e:MouseEvent):void{
if (selectedDay != null){
selectedDay.gotoAndStop(1); //assuming frame 1 is an "unselected" state
} // you don't really need an else, if you are not going to do anything in it
selectedDay = e.target;
selectedDay.gotoAndStop(2);
}
public function getSelectedDayInfo():Object{
var _object = new Object();
_object.weekday = "nothing selected";
_object.date = "nothing selected";
if(selectedDay != null){
_object.weekday = selectedDay.weekday_txt.text;
_object.date = selectedDay.date_txt.text;
}
return (_object);
}
Hope that helps...