*EDIT – I find what is giving me the error is when i try to change the graphics properties of the movieclip.*
I am receiving this error. “ReferenceError: Error #1069: Property item not found on flash.events.MouseEvent and there is no default value. at PlayScreen/onClick()”.
I did a bit of research and most sites say it is due to a typo. but as far as i can tell it looks fine?
I have two events which are set out exactly the same as far as i can tell. One is triggered onclick and the other onhover. The hover event works fine but when i use the onclick event i recieve the error above. However even though i receive the error the function still works as it should.
Here is my code.
the code below creates a new Grid. then the custom events are added. the hover event works fine with no errors. the onclick event works fine but i get the error.
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class PlayScreen extends MovieClip {
public var grid:Grid;
public function PlayScreen() {
grid = new Grid();
grid.addEventListener( GridEvent.HOVER, onHover);
grid.addEventListener( GridEvent.TILECLICK, onClick);
grid.x = 0;
grid.y = 0;
addChild( grid );
}
public function onHover(event:*){
event.item.graphics.beginFill(0x66ff66);
event.item.graphics.lineStyle(2, 0x22ff22);
event.item.graphics.drawRect(-30*0.5, -30*0.5, 30, 30);
event.item.graphics.endFill();
}
public function onClick(event:*){
event.item.graphics.beginFill(0x000000);
event.item.graphics.lineStyle(2, 0x22ff22);
event.item.graphics.drawRect(-30*0.5, -30*0.5, 30, 30);
event.item.graphics.endFill();
}
Below is my grid class. It contains a movieclip which has two mouseEvents added to it which then trigger the custom events.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Grid extends MovieClip {
public function Grid() {
var test = new MovieClip();
test.x = 0;
test.y = 0;
test.graphics.beginFill(0x66ff66);
test.graphics.drawRect(-tileWidth*0.5, -tileHeight*0.5, tileWidth, tileHeight);
test.graphics.endFill();
test.addEventListener(MouseEvent.MOUSE_OVER, overTile);
test.addEventListener(MouseEvent.MOUSE_DOWN, clickTile);
addChild(test);
}
function overTile (event:*) {
dispatchEvent( new GridEvent( GridEvent.HOVER, event.target) );
}
function clickTile(event:*) {
dispatchEvent( new GridEvent( GridEvent.TILECLICK, event.target) );
}
}
Below are my custom events
package
{
import flash.events.Event;
public class GridEvent extends Event
{
public static const HOVER :String = "hover";
public static const TILECLICK :String = "click";
public var item;
public function GridEvent (type:String, item)
{
this.item = item;
super(type);
}
}
}
try giving the
eventand theitema type.use
Spriteinstead ofMovieClip.and another thing i didn’t implement – would be best if you created a custom Tile-class for your tiles then you could set the type of the item in your GridEvent as Tile and not as Sprite/MovieClip.
this should work ….