How to create two buttons: when one is pushed other goes up - actionscript-3

How to create two buttons: one button is pushed other goes up. I tried using this:
//Create male/female button objects
var maleButton:maleButtonObejct = new maleButtonObject();
var femaleButton:femaleButtonObject = new femaleButtonObject();
//Add evnet listeners to both buttons
maleButton.addEventListener(MouseEvent.CLICK, setToMale);
femaleButton.addEventListener(MouseEvent.CLICK, setToFemale);
//Create isMale variable
var isMale:Boolean;
//Male/Female button functions
function setToMale(event:MouseEvent):void {
isMale = true;
maleButton.stop(3);
femaleButton.stop(1);
}
function setToFemale(event:MouseEvent):void {
isMale = false;
femaleButton.stop(3);
maleButton.stop(1);
}
I get runtime error: TypeError: Error #1006: stop is not a function.
at Untitled_fla::MainTimeline/setToFemale()

Rename your classes starting with a capital letter, such as MaleButton.
Here is an easy way to accomplish what you want:
var maleButton:MaleButton = new MaleButton();
var femaleButton:FemaleButton = new FemaleButton();
maleButton.addEventListener(MouseEvent.CLICK, clicked);
maleButton.addEventListener(MouseEvent.CLICK, clicked);
function clicked(event:MouseEvent):void
{
switch(event.target)
{
case maleButton:
maleButton.gotoAndStop(3);
femaleButton.gotoAndStop(1);
break;
case femaleButton:
maleButton.gotoAndStop(1);
femaleButton.gotoAndStop(3);
break;
default:
return;
}
}

Related

Call a random function from an array

I have three functions that I have listed in an array. Now I need a random function of the three to be called when pressing a button. However, when I press the button it calls all three functions and I'm not quite sure where I've gone wrong. It looks like this right now:
function Arm1function1(){
this.parent.parent.parent.Armfront1.visible = true;
this.parent.parent.parent.Armback1.visible = false;
}
function Arm1function2(){
this.parent.parent.parent.Armfront1.visible = false;
this.parent.parent.parent.Armback1.visible = true;
}
function Arm1function3(){
this.parent.parent.parent.Armfront1.visible = false;
this.parent.parent.parent.Armback1.visible = false;
}
function getRandomElementOf(Armbuttonarray1:Array):Object {
var Armbuttonarray1:Array = [Arm1function1(), Arm1function2(), Arm1function3()];
var idx:int=Math.floor(Math.random() * Armbuttonarray1.length);
return Armbuttonarray1[idx];
}
Randombutton1part1.addEventListener(MouseEvent.CLICK, Randombutton1part1Click);
function Randombutton1part1Click(e:MouseEvent):void
{
getRandomElementOf(null);
}
Any clue of where I've gone wrong?
Your issue is this line:
var Armbuttonarray1:Array = [Arm1function1(), Arm1function2(), Arm1function3()];
When populating that array, you are actually populating it with the results of the functions.
Should be:
var Armbuttonarray1:Array = [Arm1function1, Arm1function2, Arm1function3];
Notice the lack of parenthesis ().
You want to actually execute the function on the click handler, so you'll need to tweak that a bit too:
getRandomElementOf(null)();
or
getRandomElementOf(null).call();
As an aside, your getRandomElementOf function should probably look more like this:
function getRandomElementOf(array:Array):Object {
return array[Math.floor(Math.random() * array.length)];
}
Then do:
getRandomElementOf([Arm1function1, Arm1function2, Arm1function3])();

Is there a generic ScreenSpaceEvent that captures all events?

To react to specific space handlers I typically do this -
var fooHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
fooHandler.setInputAction(function(movement){
// do stuff
}, Cesium.ScreenSpaceEventType.WHEEL);
This function would be limited to WHEEL inputs. I have a couple of things that I need to do every time the camera changes position or height. I tried creating an event handler for the camera in a fashion similar to the above, and then calling camera.positionCartographic within that function, but to no avail.
Is there an event in Cesium that captures any movement?
You don't want to use ScreenSpaceEventHandler to do this. Instead, you subscribe to the preRender event and compare the camera position from last frame. Here's some sample code for you:
var lastTime = Cesium.getTimestamp();
var lastPosition = viewer.scene.camera.position.clone();
function preRender(scene) {
var time = Cesium.getTimestamp();
var position = scene.camera.position;
if (!Cesium.Cartesian3.equalsEpsilon(lastPosition, position, Cesium.Math.EPSILON4)) {
document.getElementById('viewChanged').style.display = 'block';
lastTime = time;
} else if (time - lastTime > 250) {
//hide the 'view changed' message after 250 ms of inactivity
lastTime = time;
document.getElementById('viewChanged').style.display = 'none';
}
lastPosition = position.clone();
}
viewer.scene.preRender.addEventListener(preRender);
We plan on adding a viewChanged event to Cesium some time soon, perhaps with 1.8, but this code will continue to work after that and you'll be able to switch to the event at your leisure.
If you want a live demo of the above code, see this port of the view changed Google Earth demo we did in Cesium: http://analyticalgraphicsinc.github.io/cesium-google-earth-examples/examples/viewchangeEvent.html
Here's what I ended up doing:
_preRender = function (scene) {
var currentPosition = scene.camera.position;
if (!Cesium.Cartesian3.equalsEpsilon(_lastPosition, currentPosition, Cesium.Math.EPSILON4)) {
_lastPosition = currentPosition.clone();
if (typeof _positionChangeTimeout !== 'undefined' && _positionChangeTimeout !== null)
{
clearTimeout(_positionChangeTimeout);
}
var currentPositionCartographic = scene.camera.positionCartographic;
_positionChangeTimeout = setTimeout(function() {
if (typeof _positionChangeListener === 'function' && _positionChangeListener !== null)
{
_positionChangeListener({
lat: Cesium.Math.toDegrees(currentPositionCartographic.latitude),
long: Cesium.Math.toDegrees(currentPositionCartographic.longitude),
zoomLevel: _calcZoomForAltitude(currentPositionCartographic.height, currentPositionCartographic.latitude)
});
}
}, 250);
}
}

How do I use this URLRequest script?

I started learning ActionScript 3 a week ago and have stumbled across a huge learning curve. I found this script on the internet:
var _loader:URLLoader;
var _request:URLRequest;
function loadData():void {
_loader = new URLLoader();
_request = new URLRequest("http://www.travoid.com/game/Purchase.php?gid=1");
_request.method = URLRequestMethod.POST;
_loader.addEventListener(Event.COMPLETE, onLoadData);
_loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFailedToLoad);
_loader.load(_request);
}
function onLoadData(e:Event):void {
trace("onLoadData",e.target.data);
}
function onDataFailedToLoad(e:IOErrorEvent):void {
trace("onDataFailedToLoad:",e.text);
}
This all seems to work and is generating no errors or output, however my issue comes about when I use this next part of code (which I made)
function vpBuy(e:MouseEvent):void{
loadData();
if (e.target.data == "false") {
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
I get this error:
ReferenceError: Error #1069: Property data not found on
flash.display.SimpleButton and there is no default value. at
travoid_fla::MainTimeline/vpBuy() onLoadData
The part that is probably throwing this is:
if (e.target.data == "false") {
I was hoping e.target.data was what stored the value on the web page (which displays as false) but apparently not. With the code I found on the internet, what stores the information on the web page?
Thanks,
Ethan Webster.
The URLLoader load method is asynchronous, you have to wait the server response before triyng to get the result.
The functions onLoadData and onDataFailedToLoad are there to do that. When the response is well received the function onLoadData is called and you can get the data in e.target.data or _loader.data
The error in your function vpBuy is you try to access the data property on the object that triggered the MouseEvent (maybe a Button) and that object don't have such variable.
Try the following:
/** button clicked load the datas from the server **/
function vpBuy(e:MouseEvent):void
{
// load the datas from the server
loadData();
}
/** the datas are well loaded i can access them **/
function onLoadData(e:Event):void
{
trace("onLoadData",e.target.data);
if( e.target.data == "false" )
{
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
Hope this could help you :)

manually set check box selected in as3

I have a CheckBox in as3 that i need to set as checked with code. Do I dispatch an event? If so, how do I create an event, and give it a target (target is read only). Or is there another way to do it? thanks.
my_checkbox.selected = true;
And of course:
my_checkbox.selected = false;
To handle the box being checked/unchecked:
my_checkbox.addEventListener(MouseEvent.CLICK, _selected);
function _selected(e:MouseEvent):void
{
var bool:Boolean = e.target.selected;
if(bool)
{
trace("was checked");
// more code
}
else
{
trace("was unchecked");
// more code
}
}

Actionscript button linking issue

So this doesn't make any sense. I have actionscript in a flash-based button menu, and one of the buttons is linking to the wrong page, and i cannot figure out why. Here is the actionscript:
var myURL1:URLRequest = new URLRequest ("home.html");
home_btn.addEventListener(MouseEvent.CLICK, home_btnEventHandler);
function home_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL1, "_self");
}
var myURL2:URLRequest = new URLRequest ("featuredwork.html");
work_btn.addEventListener(MouseEvent.CLICK, work_btnEventHandler);
function work_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL2, "_self");
}
var myURL3:URLRequest = new URLRequest ("featuredartist.html");.
artist_btn.addEventListener(MouseEvent.CLICK, artist_btnEventHandler);
function artist_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL3, "_self");
}
var myURL4:URLRequest = new URLRequest ("artists.html");
members_btn.addEventListener(MouseEvent.CLICK, members_btnEventHandler);
function members_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL4, "_self");
}
var myURL5:URLRequest = new URLRequest ("events.html");
events_btn.addEventListener(MouseEvent.CLICK, events_btnEventHandler);
function events_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL5, "_self");
}
var myURL6:URLRequest = new URLRequest ("/blog/index.php");
blog_btn.addEventListener(MouseEvent.CLICK, events_btnEventHandler);
function blog_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL6, "_self");
}
Now, when I click on blog_btn, it is sending me to the "events" page. It makes no sense. Does anybody have any idea?
Fairly easy to spot: you have
blog_btn.addEventListener(MouseEvent.CLICK, events_btnEventHandler);
when you mean
blog_btn.addEventListener(MouseEvent.CLICK, blog_btnEventHandler);
notice the second parameter.
You've bound the events handler to the blog_btn click - change the last block to point to the correct handler:
blog_btn.addEventListener(MouseEvent.CLICK, blog_btnEventHandler);