actionscript 2 functions for detecting when no keys are pressed - button

I would like to know how to detect when no buttons are pressed on a movieclip,
such as
if (no keys are pressed){
this.gotoAndStop("idle");
}

Firstly if you expand your question as you expand your purpose of what you achieve, maybe I can present a more in depth solllution ... ;) I can not imagine why you need a no buttons pressed event. Anyway you can use this code snippet.
//This belongs to no button pressed event.
var def = 0;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
def = 1;
};
keyListener.onKeyUp = function() {
def = 0;
};
Key.addListener(keyListener);
//First Part for the whole movie clip
_root.onEnterFrame = function (){
trace(def);
if(def == 1){
this.gotoAndStop("idle");
}
}

Related

Adobe ExtendScript Javascript: Replacing string with a function containing UI elements

New to coding in general, was wondering if its possible to change a content of a variable and update it to the UI with replace() or change()? I believe its much more harder than that but I can't wrap my head around it.
(function(thisObj) {
scriptBuildUI(thisObj)
function scriptBuildUI(thisObj) {
var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "replaceTest", undefined, 0);
win.alignChildren = ['center','top'];
var myTab = win.add("panel", undefined);
var replacebutton = myTab.add("button",undefined,"Replace");
var myTab2 = "x";
replacebutton.onClick = function(){ myTab2.replace("x",function(){hO()}) };
function hO() {myTab.add("button", undefined,"Retgf")};
win.onResizing = win.onResize = function() {
this.layout.resize();
};
win instanceof Window
? (win.center(), win.show()) : (win.layout.layout(true), win.layout.resize());
}
})(this);
I am not sure what you try to do here. myTab2 is a string variable and .replace() is a string function there. It returns a string. So you are trying to replace the "x" with the content of the function? To what end?
Also, the way i see it, that would just add another button to myTab.
If you could tell me what you try to do, maybe we can find a work-around.
Edit: To just assign your function to myTab2, you can directly do that. And if you want changes to your UI, i think you need to run win.layout.layout(true); to make them vissible. The folowing code adds a button to your tab every time it's clicked.
(function(thisObj) {
scriptBuildUI(thisObj)
function scriptBuildUI(thisObj) {
var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "replaceTest", undefined, 0);
win.alignChildren = ['center','top'];
var myTab = win.add("panel", undefined);
var replacebutton = myTab.add("button",undefined,"Replace");
var myTab2 = "x";
replacebutton.onClick = function(){ myTab2=hO() };
function hO() {
myTab.add("button", undefined,"Retgf");
win.layout.layout(true);
};
win.onResizing = win.onResize = function() {
this.layout.resize();
};
win instanceof Window
? (win.center(), win.show()) : (win.layout.layout(true), win.layout.resize());
}
})(this);

creating a Placemarks that can be hidden

I have been trying to create a Placemark that I can hide and show (like turning visibility on and off) on demand (on click)... I am using this to make the placemark:
function placemark(lat, long, name, url, iconsrc){
var placemark = ge.createPlacemark(name);
ge.getFeatures().appendChild(placemark);
placemark.setName(name);
// Create style map for placemark
var icon = ge.createIcon('');
if(iconsrc == "0")
icon.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
else{
icon.setHref(iconsrc);
}
var style = ge.createStyle('');
style.getIconStyle().setIcon(icon);
if(iconsrc != "0")
style.getIconStyle().setScale(2.5);
placemark.setStyleSelector(style);
// Create point
var point = ge.createPoint('');
point.setLatitude(lat);
point.setLongitude(long);
//point.setAltitudeMode(1500);
placemark.setGeometry(point);
google.earth.addEventListener(placemark, 'click', function(event) {
// Prevent the default balloon from popping up.
event.preventDefault();
var balloon = ge.createHtmlStringBalloon('');
balloon.setFeature(placemark); // optional
balloon.setContentString(
'<iframe src="'+ url +'" frameborder="0"></iframe>');
ge.setBalloon(balloon);
});
}
I have tried everything... from this:
function hidePlacemark(name){
var children = ge.getFeatures().getChildNodes();
for(var i = 0; i < children.getLength(); i++) {
var child = children.item(i);
if(child.getType() == 'KmlPlacemark') {
if(child.getId()== name)
child.setVisibility(false);
}
}
}
to using this ge.getFeatures().removeChild(child);
can anyone point me to the right direction on creating a function that will allow me to turn the visibility on/off on demand please.
Your hidePlacemark function is missing some {} in your final IF statement
if(child.getId()== name)
you have
function hidePlacemark(name){
var children = ge.getFeatures().getChildNodes();
for(var i = 0; i < children.getLength(); i++) {
var child = children.item(i);
if(child.getType() == 'KmlPlacemark') {
if(child.getId()== name)
child.setVisibility(false);
}
}
}
make it
function hidePlacemark(name){
var children = ge.getFeatures().getChildNodes();
for(var i = 0; i < children.getLength(); i++) {
var child = children.item(i);
if(child.getType() == 'KmlPlacemark') {
if(child.getId()== name) {
child.setVisibility(false);
}
}
}
}
HOWEVER ------- you are better off doing this as it is much faster as you don't need to loop through ALL your placemarks
function hidePlacemark(name) {
var placemark = ge.getElementById(name);
placemark.setVisibility(false);
}
I think the plain ge.getFeatures().removeChild(placemark); works.
I played with this GooglePlayground, and just added the following code to line 8 (that is empty in this GooglePlayground Sample):
addSampleButton('Hide Placemark', function(){
ge.getFeatures().removeChild(placemark);
});
Clicking the button Hide Placemark hides the placemark like a charm here. Any chances your problem is somewhere else in your code?

Music player in Actionscript 3.0

I already added a stop button with autoplay but I need to make it so when you click the button again after you had stopped it, the music starts playing.
Source code:
var music:Sound = new Sound(new URLRequest("calmingsong.mp3"));
var sc:SoundChannel = music.play();
button1.addEventListener(MouseEvent.CLICK, stopMusic);
function stopMusic(e:Event):void
{
sc.stop();
}
If you just want to play the sound over from the beginning, just call the Sound object's play() method again (you get a new SoundChannel object when you do this).
If you'd like to resume playing the sound at the point where the user stopped it, you'll need to add additional variables to store the current "playback state"... Something like this:
var music:Sound = new Sound(new URLRequest("calmingsong.mp3"));
var sc:SoundChannel = music.play();
var startPosition:Number = 0;
var isPlaying = true; // default to true cause you auto play...
button1.addEventListener(MouseEvent.CLICK, togglePlayback);
function togglePlayback(e:Event):void
{
if (isPlaying)
{
startPosition = sc.position;
sc.stop();
isPlaying = false;
}
else
{
sc = music.play(startPosition);
isPlaying = true;
}
}

Flex: assigning events to dynamically created buttons

Thanks to wezzy and the others who helped me out the past couple of days. I've been taking their advice and trying to figure the rest out on my own but I'm stuck again.
I have a txt file structured like this:
button1_label
button2_label
button3_label
My program creates these 3 buttons at runtime and places them in a group.
protected function onLoaded(e:Event):void {
var myArrayOfLines:Array = e.target.data.split(/\n/);
var tempBtn:Button;
for(var i:Number = 0;i < myArrayOfLines.length;i++){
var j:Number = i+1;
tempBtn = new Button();
tempBtn.id = "btn" + i;
tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
var index:uint = parseInt(evt.currentTarget.id.replace("btn", ""));
//TextArea code will go here
trace(text); // Traces null
});
tempBtn.label = myArrayOfLines[i];
btnArray.push(tempBtn);
group.addElement(btnArray[i]);
}
}
Now what you can see from my code is that I'm attempting to make each button print a string to a textarea. I've structured my new buttons.txt like this:
button1_label
"Hello"
button2_label
"Goodbye"
button3_label
"Come again"
So what I want to do is have button1 print "Hello". All the lines of the .txt file are pushed into myArrayOfLines. Thanks in advance for any help.
EDIT
Full explanation
Sorry I was trying to simplify my question, guess I made it harder to understand. I made a text editor than runs client side, no server. I have a set of buttons that insert predefined phrases into the TextArea Each button has a listener that does myTextArea.insert("sometext"); (but different text for each button. Users have requested the ability to create their own buttons to insert there own strings. I figured I would have a couple og textinputs where a user could define a label, and a String that would be inserted into the textarea on button click. I would write the label to one line, then the String to the next line. Right now I created a buttons.txt file with this format to see if it would work.
FINAL EDIT: WORKING CODE
public function setupBtns(e:Event):void{
var file:File = File.documentsDirectory.resolvePath("buttons.txt");
var stream:FileStreamWithLineReader = new FileStreamWithLineReader();
stream.open(file, FileMode.READ);
while(stream.bytesAvailable) {
// this line contains the headers like button1_label etc.
var label:String;
// this line contains the string
if(stream.bytesAvailable) {
// this line contains the actual label like "Hello";
label = stream.readUTFLine();
line = stream.readUTFLine();
// strip off the first and last character as they are double quotes
line = line.substring(1, line.length-1);
var tempBtn:Button = new Button();
tempBtn.addEventListener(MouseEvent.CLICK, btnListener);
tempBtn.label = label;
tempBtn.name = line;
btnArray.push(tempBtn);
for(var i:Number = 0;i<btnArray.length;i++){
group.addElement(btnArray[i]);
}
}
}
}
protected function btnListener(e:MouseEvent):void{
mainTextField.insertText(e.target.name);
trace(e.target.name);
}
Try this out and let me know how it works out...
EDIT: (try new code below)
protected function onLoaded(e:Event):void {
var myArrayOfLines:Array = e.target.data.split(/\n/);
var tempBtn:Button;
for(var i:Number = 0;i < myArrayOfLines.length;i=i+1){
var j:Number = i+1;
tempBtn = new Button();
tempBtn.id = "btn" + i;
tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
myTextArea.text += '\n' + myArrayOfLines[j];
});
tempBtn.label = myArrayOfLines[i];
btnArray.push(tempBtn);
group.addElement(btnArray[btnArray.length-1]);
}
}
first: don't use anonymous functions as listeners in actionscript - if you yo, you can't remove them later on.
instead use a class method like this:
tempBtn.addEventListener(MouseEvent.CLICK, onMouseClick);
private function onMouseClick(evt:MouseEvent):void
{
if (evt.currentTarget == button1_label)
{
// do sth for btn1
}
else if (evt.currentTarget == button2_label)
{
// do sth for btn2
}
// ...
}
edit
just saw, that you are using IDs, then just change the above code to sth like this:
if (evt.currentTarget.id == "btn1")

call inline function and pass in current item as the argument - Flex 3

Im trying to create a loop of items like this...
for each (var btn:Object in ViewButtonData)
{
// build element
var box:HBox = new HBox();
box.styleName = "lefttab";
box.width = "100%";
box.addEventListener("rollOver", HoverTab(btn.id, 1));
box.addEventListener("rollOut", HoverTab(btn.id, 0));
// add element to list
}
I would like to pass in current HBox to the 'HoverTab' function. Is there a way to do that?
James Ward's comment is correct. You can just do:
function myHandler(event:Event):void {
var myHBox:HBox = event.currentTarget as HBox;
}
That said, the answer to your question is:
box.addEventListener("rollOver", function(e:Event) { HoverTab(box, btn.id, 1); });
..spread out for more readability:
box.addEventListener("rollOver",
function(e:Event) {
HoverTab(box, btn.id, 1);
}
);
The HBox should automatically be available in the event handler via event.currentTarget.
box.addEventListener("rollOver", hoverTab(btn.id, 1));
box.addEventListener("rollOut", hoverTab(btn.id, 0));
This won't work unless hoverTab is a function that returns a function that takes an event as its sole argument. Replace it with:
box.addEventListener("rollOver", hoverTab);
box.addEventListener("rollOut", rollOutTab);
And as James mentioned, you'll get HBox from event.currentTarget
function hoverTab(event:MouseEvent):void
{
var box:HBox = HBox(event.currentTarget);
}
To get btn.id inside hoverTab, store HBoxes and btn.ids into two arrays from the for-each loop. Now you can get the index of HBox using indexOf method - btn.id will be at the same index in its array.
var boxes:Array = [];
var ids:Array = [];
for each (var btn:Object in ViewButtonData)
{
// build element
var box:HBox = new HBox();
boxes.push(box);
ids.push(btn.id);
box.addEventListener(MouseEvent.ROLL_OVER, hoverTab);
box.addEventListener(MouseEvent.ROLL_OUT, rollOutTab);
// add element to list
}
function hoverTab(event:MouseEvent):void
{
var box:HBox = HBox(event.currentTarget);
var btnid:Number = ids[boxes.indexOf(box)];
}
Alternatively, (since ActionScript allows different types in the same array), you can push them into the same array and read the btn.id as array[array.indexOf(box) + 1]

Resources