Away3d mesh not getting mouseClick events - actionscript-3

I am just trying to get the coordinates of the mouse in a plane but the mouse event just doesnt fire.
I also have an starling instance on top of away3d that i got with this tutorial and i believe there is a problem there.
Here is my code:
public class Main extends Sprite
{
private var view3D:View3D;
private var stage3DManager:Stage3DManager;
private var stage3DProxy:Stage3DProxy;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
view3D = new View3D();
initProxies();
}
private function initProxies():void
{
stage3DManager = Stage3DManager.getInstance(stage);
stage3DProxy = stage3DManager.getFreeStage3DProxy();
stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
}
private function onContextCreated(event:Stage3DEvent):void
{
initAway3D();
initStarling();
var floor:Mesh = new Mesh(new PlaneGeometry(600, 400), new ColorMaterial(0x530000));
floor.mouseEnabled = true;
view3D.scene.addChild(floor);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
floor.addEventListener(MouseEvent3D.CLICK, onClick);
}
private function onClick(e:MouseEvent3D):void
{
trace("Click");
}
private function initAway3D():void
{
view3D.stage3DProxy = stage3DProxy;
view3D.shareContext = true;
addChild(view3D);
view3D.mousePicker = PickingType.SHADER;
view3D.camera = new Camera3D(new OrthographicLens());
view3D.camera.x = 1000;
view3D.camera.y = 1000;
view3D.camera.z = 1000;
view3D.camera.lookAt(new Vector3D(0, 0, 0));
}
private function initStarling():void
{
var starling:Starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.showStats = true;
starling.start();
}
private function onEnterFrame(e:Event):void
{
stage3DProxy.clear();
view3D.render();
stage3DProxy.present();
}
}
}

I'm working on a small project, I based it from this tutorial, and everything works fine!
I had a problem with feathers but I resolved it when I called starling.start() which is not in the tutorial, but you've already did it :)
Try this :
private function initStarling():void
{
starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.addEventListener(Event.ROOT_CREATED, rootCreatedHandler);
}
private function rootCreatedHandler(event:Event):void
{
starling.removeEventListener(Event.ROOT_CREATED, rootCreatedHandler);
stage3DProxy.addEventListener(flash.events.Event.ENTER_FRAME, onEnterFrame);
starling.start();
}
private function onEnterFrame(event:flash.events.Event):void
{
view3D.render();
starling.nextFrame();
}
I hope that it will help you!

Related

use of button to increase and decrease the timer in action script3.0

I have written a code to add two buttons. It shows them correctly, but the action is not performed correctly.
public class butt extends Sprite {
public function butt() {
var delayGlobal:Number = 2000;
var min1:Number =1000;
var myTimer:Timer = new Timer(delayGlobal);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
// Button Event
myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
function myButton1Click(ev:MouseEvent):void {
delayGlobal = delayGlobal- 1000;
trace(delayGlobal);
}
myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
function myButton2Click(ev:MouseEvent):void {
delayGlobal = delayGlobal + 1000;
trace(delayGlobal);
}
function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http://google.com.example2";
loader.load(new URLRequest(url));
addChild(loader);
}
}
}
The timer is shown but doesn't work
Your issue is that you are updating a variable, but not actually updating the timer at all.
You need to explicitly tell the timer to change the delay. myTimer.delay = delayGlobal
Here is a re-working of your code:
public class butt extends Sprite {
private var min1:Number =1000; //assuming this is your minimum allowed delay?
private var myTimer:Timer;
public function butt() {
myTimer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
// Button Event
myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
}
private function myButton1Click(ev:MouseEvent):void {
myTimer.delay = Math.max(min1, myTimer.delay - 1000); //This will assign whatever is bigger, min1 or the timer delay less 1000 - ensuring that the timer doesn't drop below the value of min1
trace(myTimer.delay);
}
private function myButton2Click(ev:MouseEvent):void {
myTimer.delay = myTimer.delay + 1000;
trace(myTimer.delay);
}
private function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http://google.com.example2";
loader.load(new URLRequest(url));
addChild(loader);
}
}

how to access dynamic/static movieclip with linked class?

hi this question still bothering me. It looks simple.
I got movieclips in the lib and on stage which has a link-class "Box.as" and another which linked to "Circle.as".
I want to access the movieclip of the Box.as from the Circle.as or vice-versa.
public class Circle extends MovieClip
{
private var _circle:MovieClip;
private var _box:Box;
public function Circle()
{
_circle = new MovieClip();
if (stage) onStage();
else this.addEventListener(Event.ADDED_TO_STAGE,onStage);
}
private function onStage(e:Event = null)
{
_circle = stage.getChildByName("blue_circle") as MovieClip;
this.addEventListener(Event.ENTER_FRAME,hitTarget);
}
private function hitTarget(e:Event):void
{
if (_circle.hitTestObject(_box.mc)) //test if 2 movieclips are colliding
{ // _box.mc is just created the same as _circle
trace("hi");
}
}
this code ain't workin. And I wanted to use one that can access even if the movieclip wasn't on stage(which has no instance name).
Hope you can help me. Thanks.
Looks like you're really close! You just forgot to create a new instance of your class Box. So inside your public function Circle() just add
_box = new Box();
Let me know if that works. If it doesn't, there might be something wrong with your linking...
Your whole code will look like this
public class Circle extends MovieClip
{
private var _circle:MovieClip;
private var _box:Box;
public function Circle()
{
_box = new Box();
_circle = new MovieClip();
if (stage) onStage();
else this.addEventListener(Event.ADDED_TO_STAGE,onStage);
}
private function onStage(e:Event = null)
{
_circle = stage.getChildByName("blue_circle") as MovieClip;
this.addEventListener(Event.ENTER_FRAME,hitTarget);
}
private function hitTarget(e:Event):void
{
if (_circle.hitTestObject(_box.mc)) //test if 2 movieclips are colliding
{ // _box.mc is just created the same as _circle
trace("hi");
}
}

Sound recording with timer event

I got this code for recording users sound:
public class Main extends Sprite
{
private var mic:Microphone;
private var waveEncoder:WaveEncoder = new WaveEncoder();
private var recorder:MicRecorder = new MicRecorder(waveEncoder);
private var recBar:RecBar = new RecBar();
private var tween:Tween;
private var fileReference:FileReference = new FileReference();
public function Main():void
{
recButton.stop();
activity.stop();
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 100;
mic.setLoopBack(true);
mic.setUseEchoSuppression(true);
Security.showSettings("2");
addListeners();
}
private function addListeners():void
{
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
recorder.addEventListener(RecordingEvent.RECORDING, recording);
recorder.addEventListener(Event.COMPLETE, recordComplete);
activity.addEventListener(Event.ENTER_FRAME, updateMeter);
}
private function startRecording(e:MouseEvent):void
{
if (mic != null)
{
recorder.record();
e.target.gotoAndStop(2);
recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);
addChild(recBar);
tween = new Tween(recBar,"y",Strong.easeOut, -recBar.height,0,1,true);
}
}
private function stopRecording(e:MouseEvent):void
{
recorder.stop();
mic.setLoopBack(false);
e.target.gotoAndStop(1);
recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);
}
private function updateMeter(e:Event):void
{
activity.gotoAndPlay(100 - mic.activityLevel);
}
private function recording(e:RecordingEvent):void
{
var currentTime:int = Math.floor(e.time / 1000);
recBar.counter.text = String(currentTime);
if (String(currentTime).length == 1)
{
recBar.counter.text = "00:0" + currentTime;
}
else if (String(currentTime).length == 2)
{
recBar.counter.text = "00:" + currentTime;
}
}
private function recordComplete(e:Event):void
{
fileReference.save(recorder.output, "recording.wav");
}
}
I want to replace mouse events with timer event. If lasted time == 5 then start recording and after
10 seconds stop recording. I confused where add my timer code something like this:
var myIntrotime:Timer = new Timer(1000,5);
myIntrotime.addEventListener(TimerEvent.TIMER, startIntroTime);
myIntrotime.start();
var SecondsElapsed:Number = 1;
function startIntroTime(event:TimerEvent):void
{
if (SecondsElapsed==5)
{
//start recording
//start another timer2 and if timer2 finished stop recording
}
SecondsElapsed++;
}
Could someone help me?
You can do better if you employ flash.utils.setTimeout() to make a delayed call. This makes all the dirty work with timers for you.
setTimeout(startIntroTime,5000)
function startIntroTime():void
{
//start recording
setTimeout(stopRecording,10000);
}
The manual on setTimeout()

Adding object to another sprite layer causes issues

Essentially, the spawning ships appear above the crosshair whereas I want it to be the other way around. I tried adding the crosshair to another layer but then clicking / 'shooting' the ships does nothing. Any ideas?
public class Main extends MovieClip {
public static var backgroundLayer:Sprite = new Sprite();
public static var gameLayer:Sprite = new Sprite();
public static var interfaceLayer:Sprite = new Sprite();
public static var menuLayer:Sprite = new Sprite();
public var mainMenu:menuMain = new menuMain();
public var intro:IntroSound = new IntroSound();
public var soundControl:SoundChannel = new SoundChannel();
public var crosshair:crosshair_mc;
static var enemyArray:Array = [];
private var enemyShipTimer:Timer;
private var enemyShipTimerMed:Timer;
private var enemyShipTimerSmall:Timer;
public function Main()
{
addMenuListeners();
addChild(gameLayer);
addChild(backgroundLayer);
addChild(interfaceLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
interfaceLayer.addChild(howtoPlay);
interfaceLayer.addChild(gameEnd);
interfaceLayer.addChild(gameAbout);
soundControl = intro.play(0, 100);
stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function update(e:Event):void
{
for each (var enemy:EnemyShip in enemyArray)
{
enemy.update();
if (enemy.dead)
{
enemy.kill();
}
}
}
private function mouseDown(e:MouseEvent):void
{
if (e.target.name) {
switch (e.target.name) {
case "enemy_big":
updateScore(5);
e.target.parent.damage();
break;
case "enemy_medium":
updateScore(10);
e.target.parent.damage();
break;
case "enemy_small":
updateScore(15);
e.target.parent.damage();
break;
}
}
}
function addMenuListeners():void
{
//Code to add event listeners
}
public function startGame(e:Event)
{
removeMenuListeners();
soundControl.stop();
if (howtoPlay.parent == interfaceLayer)
{
interfaceLayer.removeChild(howtoPlay);
}
if (gameAbout.parent == interfaceLayer)
{
interfaceLayer.removeChild(gameAbout);
}
if (gameEnd.parent == interfaceLayer)
{
interfaceLayer.removeChild(gameEnd);
}
if (mainMenu.parent == menuLayer)
{
menuLayer.removeChild(mainMenu);
}
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
gameLayer.addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
}
function spawnEnemy(type:String, speed:Number) {
var enemy = new EnemyShip(type, speed);
enemyArray.push(enemy);
gameLayer.addChild(enemy);
return enemy;
}
function sendEnemy(e:TimerEvent):void
{
Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
spawnEnemy("big", Math.random() * 5 + 12);
}
function sendEnemyMed(e:TimerEvent):void
{
Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
spawnEnemy("medium", Math.random() * 7 + 14);
}
function sendEnemySmall(e:TimerEvent):void
{
Timer(e.currentTarget).delay = (1+Math.random()*2)*1000;
spawnEnemy("small", Math.random() * 9 + 16);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
scoreText.setTextFormat(scoreFormat);
}
static function removeEnemy(enemyShip:EnemyShip):void {
enemyArray.splice(enemyArray.indexOf(enemyShip), 1);
gameLayer.removeChild(enemyShip);
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
I expect MouseDowns are intercepted by the parent layer. You have employed mouseEnabled=false for crosshair, now you should do that for its parent.
public static var crosshairLayer:Sprite=new Sprite();
public function Main() {
... // initialize everything first
addChild(gameLayer);
addChild(crosshairLayer);
crosshairLayer.mouseEnabled=false;
crosshairLayer.mouseChildren=false;
}
Then you add crosshair to crosshairLayer.

AS3 mouse events not firing when mouseX = 0

I'm creating what should be a very simple, full screen drag and drop game in Flash Develop. It works fine except in one frustrating instance.
I add items to the stage, add MOUSE_DOWN listeners to them and start dragging when one hears that listener. I then add a MOUSE_UP listener to figure out when to stop the drag. Again, this works fine unless mouseX = 0. When the mouse is all the way to the left of the screen and I mouse up or mouse down no listener is fired. I also took it out of full screen mode and if the mouse is at or below 0 no mouse events will fire.
What in the world is going on?
private function itemSelectedHandler(e:MouseEvent):void
{
thisItem = GameItem(e.currentTarget);
thisItem.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, itemUnselectedHandler, false, 0, true);
}
private function itemUnselectedHandler(e:MouseEvent):void
{
stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, itemUnselectedHandler);
thisItem.removeEventListener(MouseEvent.MOUSE_DOWN, itemSelectedHandler);
}
You are calling stopDrag on the class and not the dragged sprite. Try something like the following :
package
{
public class Main extends Sprite
{
private var _draggedSprite:Sprite = null;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
for (var i:int = 0; i < 10; i++)
{
createBox();
}
}
private function createBox():void
{
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xff0000, 1);
sp.graphics.drawRect(0, 0, 30, 30);
sp.graphics.endFill();
sp.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
sp.x = Math.random() * (stage.stageWidth - 30);
sp.y = Math.random() * (stage.stageHeight - 30);
addChild(sp);
}
private function onMouseDown(e:MouseEvent):void
{
var sp:Sprite = e.target as Sprite;
sp.startDrag();
_draggedSprite = sp;
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseUp(e:MouseEvent):void
{
_draggedSprite.stopDrag();
_draggedSprite = null;
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
}
}
This worked for me when mouseX=0 in fullscreen mode.