Need proper format in code - actionscript-3

I am putting in a code that when the user presses a particular button on the keyboard then that color will paint an object on the the flash stage; but it doesn't work - here is my code:
if (e.keyCode == "G".charCodeAt()) {
colorNumber = #04FA00;
} else if (e.keyCode == "H".charCodeAt()) {
colorNumber = #CA00E3;
}

If e is a KeyboardEvent, you may use e.charCode() == 'G'.

Hex numbers for colors are written as "0x000000" and not "#000000". So you would write:
colorNumber = 0x04FA00;
If you use
e.charCode == "G".charCodeAt(0);
You would need to press shift-g for it to work. Otherwise use
e.charCode == "g".charCodeAt(0);

Related

How can I capture if the user clicked a wrong button?

I made a game remember for the first fishes. I found a code like this. I also have wrong buttons.
Wrong buttons list:
pinkButton.addEventListener(MouseEvent.CLICK, pinkClick);
whiteButton.addEventListener(MouseEvent.CLICK, whiteClick);
greyButton.addEventListener(MouseEvent.CLICK, greyClick);
And I use this code
import flash.events.MouseEvent;
var checkString:String = "";
 
//Create event listeners and their functions.
YellowButton.addEventListener(MouseEvent.CLICK, yellowClick);
RedButton.addEventListener(MouseEvent.CLICK, redClick);
BlueButton.addEventListener(MouseEvent.CLICK, blueClick);
/*True choices*/
function yellowClick(evt:Event):void
{
//In each event listener function, add a letter or
//string to the checkString variable.
checkString += "y";
//Then, see if the string matches or not.
check();
}
function redClick(evt:Event):void
{
checkString += "r";
check();
}
function blueClick(evt:Event):void
{
checkString += "b";
check();
}
/*True choices*/
 
//If the proper sequence is red, yellow, blue, the string would read "ryb".
function check():void
{
if(checkString == "ryb")
{
//Clear the checkString for convenience before going on.
clearString();
//CODE TO GO TO NEW FRAME
gotoAndStop(3);
}
else
{
//Make sure the string is at least 3 characters long.
if(checkString.length >= 3)
{
clearString();
gotoAndStop(1);
}
}
}
function clearString():void
{
//You will want to have a function for clearing the string.
//This is especially useful if you have a button for "start over."
checkString = "";
}
 
if I click yellow, red, blue it works. How can I make wrong choices? Do I have to write a code for all possibilities? Player has 1 chance. For example if player clicked 2 false and 1 true button,or 2 true and 1 false, this results in a loss for the player.
Use an array of values. Like
var correctSequence:Array = ["r", "y", "b"];
then have a incrementing variable to give you control over traversing the array
var arrayPosition:int = 0;
and you need a variable to hold the Boolean value of whether or not there have been any wrong guesses:
var noneWrong:Boolean = true;
Then you could do something like
private function playerNextGuess(e:MouseEvent):void{
if (e.target._color == correctSequence[arrayPosition] && noneWrong == true){
arrayPosition++;
if (arrayPosition == 3){
playerWin();
arrayPosition = 0;
}
} else {
// put whatever logic you want for when the guess is wrong
noneWrong = false;
arrayPosition++;
if (arrayPosition == 3){
playerLose();
arrayPosition = 0;
}
}
This will make it so that 3 guesses are made before the results (right or wrong) are given to the player. But it won't tell the player which were right and which were wrong. If none are wrong, the win function is called. If any are wrong the lose function is called. Is that what you wanted?
Hopefully that gets you going in the right direction. Let me know if anything I wrote isn't abundantly clear.

AS3 Custom Cursor On Click

I'm working on a painting game, that once you click on the brushes, the mouse switches to the graphical counterpart of said brushes and will let you paint on screen. If no brush has been selected, the mouse will remain the same.
The Rectangle and the brushes are on a separate Movieclip, which allows me to layer png lines over it so you can fill in and draw.
In the actions layer in the Scene 1, this is my code for changing the mouse:
var cursor_mc:MovieClip;
if (CanvPark_mc.HugeSelected1 == true){
cursor_mc = cursor1_mc;
}else if(CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc;
}else if(CanvPark_mc.SmallSelected1 == true) {
cursor_mc = cursor3_mc;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCursor);
function moveCursor(myEvent:MouseEvent) {
if(CanvPark_mc.SmallSelected1 == false, CanvPark_mc.MediumSelected1 == false, CanvPark_mc.HugeSelected1 == false)
{ Mouse.cursor="auto";
}else{
setChildIndex(cursor_mc, this.numChildren-1);
cursor_mc.x = (mouseX);
cursor_mc.y = (mouseY);
Mouse.hide();
}
}
Each brush has a boolean variable associated to it: Small, Medium and HugeSelected1, so that way, I can tell at all times in code which one is selected and which one isn't.
Right now, running this code, in the start, nothing happens, but if I click any of the brushes, this pops up in the output.
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/setChildIndex()
at visibilityToggle/moveCursor()[visibilityToggle::frame1:42]
Seems to be pointing specifically at
setChildIndex(cursor_mc, this.numChildren-1);
I honestly don't know what's causing this error. I thought it would be this straight forward to change my mouse cursor.
How can I fix this?
Regarding the error you posted in your first post - such error occurs when some object you work with is null, i.e. not initialized or already destroyed. The error usually generalized to NPE (null pointer exception). When such error occurs, you should check if all your objects exist.
The second error occurs because your cursor_mc doesn't have parent clip (i.e. it wasn't added to stage) or the parent object is not the same object you call the setChildIndex. I suggest reading this doc
To solve the second problem you can check if the parent clip actually exists. Also, keep in mind, that if you have reassigned a value of cursor_mc you need to add it to stage again and, perhaps, you want to remove the previous clip from stage (assuming that cursor1_mc, cursor2_mc, cursor3_mc are not on stage.)
Here is a rough example:
var cursor_mc:MovieClip;
if (CanvPark_mc.HugeSelected1 == true){
cursor_mc = cursor1_mc;
}else if(CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc;
}else if(CanvPark_mc.SmallSelected1 == true) {
cursor_mc = cursor3_mc;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCursor);
function moveCursor(myEvent:MouseEvent) {
if(CanvPark_mc.SmallSelected1 == false && CanvPark_mc.MediumSelected1 == false && CanvPark_mc.HugeSelected1 == false)
{
Mouse.cursor="auto";
}
else if (cursor_mc)
{
addChild(cursor_mc);
setChildIndex(cursor_mc, this.numChildren-1);
cursor_mc.x = (mouseX);
cursor_mc.y = (mouseY);
Mouse.hide();
}
}

Break keydown event for div movement with different keydown event jQuery

I've written a script that moves a div with the arrow keys. This works as I want with the only exception that it only switches to a new direction if there was a keyup before the immediately following keydown event.
In other words, the user cannot change directions as without releasing all keys briefly.
This is my code for the "down" and "right" movement:
$(document).keydown(function(e){
var code = e.keyCode || e.which;
count++;
if(count < 2){
switch(code){
case 39:
right();
break
case 40:
down();
break
}
}
}).keyup( function(e) {
var code = e.keyCode || e.which;
count = 0
if(code == 37 || code == 38 || code == 39 || code == 40){
cancelAnimationFrame(animationframeID);
}
})
//movement functions
function down(){
animationframeID = requestAnimationFrame(down);
$("#player").css({
top: "+=5"
})
}
function right(){
animationframeID = requestAnimationFrame(right);
$("#player").css({
left: "+=5"
})
}
How can I change this script to allow a direction change to the right if the user is still pressing down? I tried to change the keyup to a keydown but that makes the movement of the div less smooth.

AS3 AIR - addEventListener that is in effect when the window is not in focus

this is with Flash cs6 trying to make an air app.
Hi, I'm fairly sure this is easy to answer when you know the expression.
so I want to know how to detect a KEY_DOWN and KEY_UP when the window is out of focus.
var winkeyDown:Boolean = false;
var altkeyDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyisDown);
stage.nativeWindow.addEventListener(KeyboardEvent.KEY_UP,keyisUp);
function keyisDown(e){
trace(e.keyCode)
if(e.keyCode == 91){
winkeyDown = true
}
if(e.keyCode == 15){
altkeyDown = true
}
if(winkeyDown && altkeyDown){
stage.nativeWindow.activate()
trace(stage.nativeWindow.x)
}
}
function keyisUp(e){
if(e.keyCode == 91){
winkeyDown = false
}
if(e.keyCode == 15){
altkeyDown = false
}
}
this only trace e.keyCode when the window is in focus.
This is obviously because of stage. but I've been searching around for hours and can only find stuff about how to force focus and text field focus and such.
what should I use for it to detect what key is down when the window isn't in focus?

Starling onClick method

I'm using starling framework, to simulate onclick method I use this code:
if(e.getTouch(this).phase == TouchPhase.ENDED){
//Some code
}
It's OK, but it also fires, if the mouse is not over the button anymore, however I'd like it to dispatch only if it's over. Is there any way to achieve this?
thanks
in the code, "this" is a Sprite, It's kinda irrelevant thougth
According to the docs the interactsWith(target:DisplayObject) method of the TouchEvent class should return true if the target is currently being touched. I have no way to test this theory, but the following should work:
if (e.getTouch(this).phase == TouchPhase.ENDED && e.interactsWith(this)) {
//The touch ended on the same DisplayObject as it originated at
}
How about this idea:
if ( e.getTouch( this ).phase == TouchPhase.ENDED ) {
if ( this.hitTestPoint( stage.mouseX, stage.mouseY, true ) ) {
// Some code
}
}
The easy way would be to use a starling.display.Button to do it. they dispatch TRIGGERED Events, which are basically what you want. The 'not so easy' way is to keep track of your touches, by replicating what is actually done in Button :
private function onTouch(event:TouchEvent):void
{
var touch:Touch = event.getTouch(this);
if (!mEnabled || touch == null) return;
if (touch.phase == TouchPhase.BEGAN && !mIsDown)
{
//equivalent to MOUSE_DOWN
mIsDown = true;
}
else if (touch.phase == TouchPhase.MOVED && mIsDown)
{
// reset button when user dragged too far away after pushing
var buttonRect:Rectangle = getBounds(stage);
if (touch.globalX < buttonRect.x - MAX_DRAG_DIST ||
touch.globalY < buttonRect.y - MAX_DRAG_DIST ||
touch.globalX > buttonRect.x + buttonRect.width + MAX_DRAG_DIST ||
touch.globalY > buttonRect.y + buttonRect.height + MAX_DRAG_DIST)
{
mIsDown = false;
}
}
else if (touch.phase == TouchPhase.ENDED && mIsDown)
{
mIsDown = false;
//this is a click
dispatchEventWith(Event.TRIGGERED, true);
}
}
you'l have to change the buttonRect code to reflect your sprite's shape, but basically here you are.