HitTestPoint not working, collisions not happening - actionscript-3

I'm trying to create something similar to Impossible Quiz Question 5 (1st quiz). However the hitTestPoint appears to be not reading. I'm not exactly sure where my error is.
Here is my full line of code.
stop();
blueTarget.addEventListener(MouseEvent.MOUSE_OVER, mousehandler2);
function mousehandler2(e:MouseEvent):void {
if (blueTarget.hitTestPoint(mouseX,mouseY,true)) {
removeEventListener(MouseEvent.MOUSE_OVER, mousehandler2);
gotoAndStop("lose");
}
}
nexttButton.addEventListener(MouseEvent.MOUSE_DOWN, mousehandler3);
function mousehandler3(e:MouseEvent):void {
removeEventListener(MouseEvent.MOUSE_DOWN, mousehandler3);
MovieClip(root).nextFrame();
}
Thanks for the help!

Do you want the player to lose if he passes the mouse over the blueTarget? If so, you can remove your if statement because the mouse over event is already added to the blueTarget

Related

Actionscript delay inside function

I created a button in Adobe Animate which should, if you press him be exchanged with another button in a different color, after 1 second you should be forwarded to the previous scene.
My Code:
button_answer_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
function fl_ClickToGoToPreviousScene(event:MouseEvent):void
{
button_answer_2.visible = false;
button_answer_2_red.visible = true;
setTimeout(myDelayedFunction,3000);
function myDelayedFunction(){
MovieClip(this.root).prevScene();
}
}
sadly the myDelayedFunction doesn't work because it is inside another fuction, I can't see analternative way. Could anyone help me? (I am not the best programmer so keep it simple if possible)
Thank you for your answer
Then why not simply move the function out of the other one. There was never a reason to nest them to begin with.
button_answer_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene);
function fl_ClickToGoToPreviousScene(event:MouseEvent):void
{
button_answer_2.visible = false;
button_answer_2_red.visible = true;
setTimeout(myDelayedFunction,3000);
}
function myDelayedFunction():void
{
MovieClip(this.root).prevScene();
}
Use inline function and test again, I can not test it now:
setTimeout(function(){ MovieClip(this.root).prevScene(); }, 3000);

AS3: Fast hovering doesn't execute rollOut

I'm having a serious problem that is getting me nervous:
I've made a button _btn that includes ROLLOVER and ROLLOUT animations with coding (an nested movieclip instance called barra that increases to half alpha when you hover over and decreases when you hover out).
[Here it should go a descriptive image but I'm new and I need 10 reputation. I'll appreciate your help]
This works perfectly but the problem occurs when I move my cursor very quickly from one point to another, with the button in between. It seems that the ROLLOUT function is not detected, so the ROLLOVER animation keeps working (and if you look carefully, the animation stops for a few seconds and then continues).
[Here it should go another descriptive image too]
This is the code in the Actions layer:
//Funciones ROLL OVER
function _btnOver(event:MouseEvent):void {
_btn.buttonMode = true;
_btn.addEventListener(Event.ENTER_FRAME,_btnFadeIn);
}
function _btnFadeIn(event:Event):void {
_btn.barra.alpha += 0.1;
if (_btn.barra.alpha >= 0.5)
{
_btn.removeEventListener(Event.ENTER_FRAME,_btnFadeIn);
}
}
_btn.addEventListener(MouseEvent.ROLL_OVER,_btnOver);
//Funciones ROLL OUT
function _btnOut(event:MouseEvent):void {
_btn.addEventListener(Event.ENTER_FRAME,_btnFadeOut);
}
function _btnFadeOut(event:Event):void {
_btn.barra.alpha -= 0.1;
if (_btn.barra.alpha <= 0.2)
{
_btn.removeEventListener(Event.ENTER_FRAME,_btnFadeOut);
}
}
_btn.addEventListener(MouseEvent.ROLL_OUT,_btnOut);
Click here if you want to download the FLA and SWF files, so you can see the problem clearly.
I barely know how to use ActionScript 3 (my only programming knowledge is Processing) and I don't have time now to learn it from head to toe, but I've researched about the problem and it's still not clear.
With tutorials and guides, I managed to learn how to create and understand this code, and I think the problem might be in the functions of the events ROLL_OVER and ROLL_OUT, which contain the addEventListener of the ENTER_FRAME events (where the animations actually are), respectively. But I don't know exactly what I have to do to fix it, what should I add or change.
I would be really glad if someone could help with this, I'm frustrated! What do you recommend me to do?
Thanks in advance
(PD: I don't understand most of the programming language. If you can be as clear and direct as possible, I'll really appreciate it)
Apparently your troubles lay in incoherent animation sequence by using enter frame listeners. You are running two independent listeners, both altering alpha of a single object, this creates a conflict, only one will work (you can determine which if you add both at once and trigger events, the resultant alpha value will indicate which listener changes it last) and you apparently expect one to do a fade in while the other to do a fade out. Instead, you should use one listener (probably even persistent) and give your object "target alpha" property as well as delta to change alpha per frame. An example:
var bbta:Number=0.2; // btn.barra's target alpha
_btn.addEventListener(Event.ENTER_FRAME,_btnFade);
function _btnFade(e:Event):void {
var a:Number=_btn.barra.alpha;
if (Math.abs(a-bbta)<1e-8) return;
// no sense of setting alpha with minuscule difference
const delta:Number=0.1; // how fast to change per frame
if (a>bbta) {
a-=delta;
if (a<=bbta) a=bbta;
} else {
a+=delta;
if (a>=bbta) a=bbta;
}
_btn.barra.alpha=a;
}
function _btnOver(event:MouseEvent):void {
_btn.buttonMode = true; // move this elsewhere, if you don't cancel buttonMode
bbta=0.5; // set target alpha, the listener will do a fade-in
}
function _btnOut(event:MouseEvent):void {
bbta=0.2; // set target alpha, the listener will do a fade-out
}
I edited some code in here, basically i am checking hover state onLoop function, so you can change your settings on here
import flash.events.Event;
var isRolledOver:Boolean = false;
//Funciones ROLL OVER
function _btnOver(event:MouseEvent):void {
isRolledOver = true;
}
function _btnOut(event:MouseEvent):void {
isRolledOver = false;
}
_btn.addEventListener(MouseEvent.ROLL_OVER,_btnOver);
_btn.addEventListener(MouseEvent.ROLL_OUT,_btnOut);
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(e){
if(this.isRolledOver){
if(_btn.barra.alpha < 0.5) _btn.barra.alpha += 0.1;
}
else{
if(_btn.barra.alpha > 0.5 || _btn.barra.alpha > 0) _btn.barra.alpha -= 0.1;
}
}
I added the sample fla in case

ActionScript , adobe flash

I have a question. I'm beginner so...
I'm creating website on flash and i have 5 pages and 5 buttons(menu buttons) and I need to use menu so I wrote this code but aobe flash writes error 1120, whats a problem?;s
thisi is code
{stop();
function projectsButton_clicked (e:MouseEvent) :void {
gotoAndStop("projects");
}
function galleryButton_clicked (e:MouseEvent) :void {
gotoAndStop("gallery");
}
function videosButton_clicked (e:MouseEvent) :void {
gotoAndStop("videos");
}
function backstageButton_clicked (e:MouseEvent) :void {
gotoAndStop("backstage");
}
function pricelistButton_clicked (e:MouseEvent) :void {
gotoAndStop("pricelist");
}
function contactButton_clicked (e:MouseEvent) :void {
gotoAndStop("contact");
}
projectsButton.addEventListener(MouseEvent.CLICK, projectsButton_clicked);
galleryButton.addEventListener(MouseEvent.CLICK, galleryButton_clicked);
videosButton.addEventListener(MouseEvent.CLICK, videosButton_clicked);
backstageButton.addEventListener(MouseEvent.CLICK, backstageButton_clicked);
pricelistButton.addEventListener(MouseEvent.CLICK, pricelistButton_clicked);
contactButton.addEventListener(MouseEvent.CLICK, contactButton_clicked);
}
Well, there are a couple of things wrong in your code:
The useless braces at the beginning and end of the program { }, but I don't think that affects much.
If you're using scenes, this is how gotoAndStop should be used: gotoAndStop(frame, "Scene name"); , unless they're all frames of a movieclip, in that case you should just use the frame number. Here you go: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001310.html
Those were just notes since those might cause errors later, the most likely cause of your error is that one or a couple of buttons mentioned in the code (projectsButton, galleryButton, videosButton, etc...) don't actually exist, they either aren't on the stage or when you were naming them on the stage you misspelled one/some of them. I think you should double check all their instance names.
That's all I can think of, best of luck with your website! ^^

Having trouble advancing frame in AS3 to advance frames

I'm creating a quiz with buttons and I'm new to ActionScript 3.0, but very familiar with 2.0.
I have a variable inside of a MC called NextQuestion. When it reaches a certain frame, it changes to 1, then back to 0.
On the main timeline, when NextQuestion == 1, it's supposed to NextFrame();. I can't get it to work though, this is the last thing I'm having trouble with.
This is my main code with 4 buttons, 3 wrong, 1 right. The feedback MC plays an animation, and when it finishes, it sets the VAR NextQuestion to 1, which is supposed to make the main timeline advance to the NextFrame.
stop();
right.addEventListener(MouseEvent.CLICK, rightClick1);
wrong1.addEventListener(MouseEvent.CLICK, wrongClick1);
wrong2.addEventListener(MouseEvent.CLICK, wrongClick1);
wrong3.addEventListener(MouseEvent.CLICK, wrongClick1);
feedback1.addEventListener(Event.ENTER_FRAME, answerRight1);
function answerRight1()
{
if (feedback1.NextQuestion == 1)
{
trace(feedback1.NextQuestion);
nextFrame();
}
else
{
trace("do nothing");
}
}
function rightClick1(ev:MouseEvent):void
{
trace(feedback1.NextQuestion);
feedback1.gotoAndPlay("right1");
}
function wrongClick1(ev:MouseEvent):void
{
trace("wrong");
feedback1.gotoAndPlay("wrong");
}
Any help is greatly appreciated! :)
The nextFrame() method in AS3 doesn't loop trough your movieClips, it just set's the movie clip to the next frame provided there is a next frame, otherwise it does nothing.
you might want to re-write the nextFrame() to:
if (totalFrames == currentFrame)
{
gotoAndStop(1); // or gotoAndPlay(1) if you need
}
else
{
nextFrame();
}
Thats regarding why the movie clip might not start from the beginning.
Another thing that look weird is that you need a FRAME_ENTER event to check if the answer is correct. You must have a lot of spam in the output log. I'd recommend you to totally ditch the ENTER_FRAME event listener for this and do it all with the CLICKED event and dispatching the event from the feedback1 object.
As much as I understand the movie clip plays when you click a button and when the animation is over you want to either progress to next frame or do nothing. If so, on the feedback1 object add a following line in the end of animation for label "right1":
dispatchEvent(new Event("right"));
and instead of the line feedback1.addEventListener(Event.ENTER_FRAME, answerRight1); have:
feedback1.addEventListener("right", answerRight1);
That way you won't need to check the answer on every frame and you don't need to check if the answer is right or not, because when the dispatchEvent(new Event("right")); fires - it's always right.
Hope I didn't misunderstand your question :)

How do I give a function to only the second frame of a movie-clip? (AS3)

I want to create a function that only allows the second frame of a movie clip to allow navigation of SWF. The "2button" is the original movie-clip while the "unlock2" is a different movie-clip on the second frame of "2button"
I tried:
//test
public function open(){
2button.unlock2.addEventListener(MouseEvent.CLICK,clickjump2);
function clickjump2(event:MouseEvent) {
gotoAndStop("play2");
}
}
Any suggestions or improvements to this would be greatly appreciated :D!
try this:
function open():void
{
2button.unlock2.addEventListener(MouseEvent.CLICK,clickjump2);
}
function clickjump2(event:MouseEvent):void
{
//check if 2button is at frame 2
if (2button.currentframe==2)
{
gotoAndStop("play2");
}
}
ps: as Versper pointed out, dont start names with numbers