Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

as3 thingy problem

Participant ,
Feb 24, 2023 Feb 24, 2023

this is supposed to make the ball go to your mouse and fall everytime you click but it's being crappy and not working more than once 

 

import flash.events.Event;
stage.addEventListener(MouseEvent.MOUSE_DOWN,gotomouse)
function gotomouse(event:Event)
{
ball.x = mouseX
ball.y = mouseY
var gravity:Number = 1
trace(gravity)

ball.addEventListener(Event.ENTER_FRAME,fall)
function fall(event:Event)
{
gravity += gravity/2
ball.y += gravity
}
}

2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 25, 2023 Feb 25, 2023

import flash.events.Event;

 

stage.addEventListener(MouseEvent.MOUSE_DOWN,gotomouse)

function gotomouse(event:Event){
ball.x = mouseX; // are you sure you don't want to animate this?
ball.y = mouseY;  //  "  "
var gravity:Number = 1
trace(gravity);

if(!ball.hasEventListener(Event.ENTER_FRAME)){

ball.addEventListener(Event.ENTER_FRAME,fall);

}
}

function fall(event:Event){
gravity += gravity/2;

ball.y += gravity;

if(ball.y>stage.stageHeight+ball.height){

ball.removeEventListener(Event.ENTER_FRAME,fall);

...
Translate
Community Expert ,
Feb 24, 2023 Feb 24, 2023

you should remove that event listener, or at least, don't keep re-adding it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 24, 2023 Feb 24, 2023

i dont know its not working. it just seems to be doubling in speed everytime i click even when i remove event listners or reset the variable or change things around

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2023 Feb 24, 2023

again, you're readding that listener and it is causing a problem.  don't do that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 25, 2023 Feb 25, 2023

again, i've tried anything and it's getting annoying because I can't get to the next step. i remove the event listeners at the beggining "access of undefined 'fall', i put the fall function outside "access of undefined 'gravity'". can you just show me other way of doing this please  I just want to make it so on MOUSE_DOWN it goes to your mouse and on MOUSE_UP it drops and accelerates. that's it. please help me

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2023 Feb 25, 2023

it's hard to know what should be done because (i assume) you're only showing part of the code your using and the code you're showing isn't coded the way one would expect for normal gravity.  so,

 

1. is that all the code related to ball?

2. do you really want ball to "fall" more and more slowly unlike gravity on planets.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 25, 2023 Feb 25, 2023

no, that is all the code. 1. yes    2. no. and "fall" is what i named my function

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2023 Feb 25, 2023

import flash.events.Event;

 

stage.addEventListener(MouseEvent.MOUSE_DOWN,gotomouse)

function gotomouse(event:Event){
ball.x = mouseX; // are you sure you don't want to animate this?
ball.y = mouseY;  //  "  "
var gravity:Number = 1
trace(gravity);

if(!ball.hasEventListener(Event.ENTER_FRAME)){

ball.addEventListener(Event.ENTER_FRAME,fall);

}
}

function fall(event:Event){
gravity += gravity/2;

ball.y += gravity;

if(ball.y>stage.stageHeight+ball.height){

ball.removeEventListener(Event.ENTER_FRAME,fall);

ball.parent.removeChild(ball);

}
}

 

*********************************************************************************************************************

/*  the above will work for one ball, but that's going to be pretty boring, so i would expect something different like clicking on one of a number of balls will trigger its fall */

 

for(var i=0;i<ballNum;i++){

this["ball_"+i].addEventListener(MouseEvent.MOUSE_DOWN,gotomouse);

}


function gotomouse(e:Event){
e.currentTarget.x = mouseX
e.currentTarget.y = mouseY
e.currentTarget.gravity = 1;

if(!e.currentTarget.hasEventListener(Event.ENTER_FRAME)){

e.currentTarget.addEventListener(Event.ENTER_FRAME,fall);

}
}

function fall(e:Event){
e.currentTarget.gravity += e.currentTarget.gravity/2;

e.currentTarget.y += e.currentTarget.gravity;

if(e.currentTarget.y>stage.stageHeight+ball.height){

e.currentTarget.removeEventListener(Event.ENTER_FRAME,fall);

e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,gotomouse);

e.currentTarget.parent.removeChild(e.currentTarget);

}
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 25, 2023 Feb 25, 2023

thanks, it looks like it work except for that other pesky problem i've been having where it says access of defined 'gravity'

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2023 Feb 25, 2023

if you use my suggested code for more than one ball, you won't have a problem. otherwise use 

 

var gravity:Number;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 25, 2023 Feb 25, 2023

i don't understand the second code and that doesn't help anything.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2023 Feb 25, 2023

then, if your project consists of one ball, use the declaration in my previous message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 25, 2023 Feb 25, 2023

i used your idea also adding in a ground collision check but when i click as it falls it doesnt reset gravity and the only way i can think do this is by removing the falling event listener everytime i click but of course it's access of undefined property

 

function setupstuff(event:Event)
{
var gravity:Number = 1
ball.removeEventListener(Event.ENTER_FRAME,setupvar);
ball.addEventListener(Event.ENTER_FRAME,fall);
function fall(event:Event)
{
gravity += gravity/2;
ball.y += gravity;
if (gravity >= 33)
{
gravity = 33
}
if(ball.hitTestObject(ground))
{
ball.removeEventListener(Event.ENTER_FRAME,fall);
ball.y += -2;
for(var i:int; i<40; i++)
{
if(ball.hitTestObject(ground))
{
ball.y += -2;
}
}
}
}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2023 Feb 25, 2023

that for loop isn't right. what's it supposed to do?

 

and why aren't you using the code i suggested?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 26, 2023 Feb 26, 2023

that for loop makes the ball get out of the ground. the code you helped me with is actually right above that

 

import flash.events.Event;
stage.addEventListener(MouseEvent.MOUSE_DOWN,gotomouse)
function gotomouse(event:Event)
{
ball.x = mouseX;
ball.y = mouseY;

if(!ball.hasEventListener(Event.ENTER_FRAME))
{
ball.addEventListener(Event.ENTER_FRAME,setupvar);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 26, 2023 Feb 26, 2023

the only reason to have a for-loop is explained above the code and is repeated below: 

 

/*  the above will work for one ball, but that's going to be pretty boring, so i would expect something different like clicking on one of a number of balls will trigger its fall */

 

anyway, if your entire project consists of one clickable ball, this should work:

 

import flash.events.Event;

 

stage.addEventListener(MouseEvent.MOUSE_DOWN,gotomouse)

function gotomouse(event:Event){
ball.x = mouseX; // are you sure you don't want to animate this?
ball.y = mouseY;  //  "  "
var gravity:Number = 1
trace(gravity);

if(!ball.hasEventListener(Event.ENTER_FRAME)){

ball.addEventListener(Event.ENTER_FRAME,fall);

}
}

function fall(event:Event){
gravity += gravity/2;

ball.y += gravity;

if(ball.y>stage.stageHeight+ball.height){

ball.removeEventListener(Event.ENTER_FRAME,fall);

ball.parent.removeChild(ball);

}
}

 

if there's some problem with this code, what problem?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 27, 2023 Feb 27, 2023

i wanted to make it so the ball stops when it hits the grounds not the bottom of the stage but i added that randomly. there's no problem with it, i just wanted to do something different

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023
LATEST

understood (and now i understand why you used hitTestObject).

 

if your ground has instance name ground, use:

 

import flash.events.Event;

 

stage.addEventListener(MouseEvent.MOUSE_DOWN,gotomouse)

function gotomouse(event:Event){
ball.x = mouseX; // are you sure you don't want to animate this?
ball.y = mouseY;  //  "  "
var gravity:Number = 1
trace(gravity);

if(!ball.hasEventListener(Event.ENTER_FRAME)){

ball.addEventListener(Event.ENTER_FRAME,fall);

}
}

function fall(event:Event){
gravity += gravity/2;

ball.y += gravity;

if(ball.hitTestObject(ground)){

ball.removeEventListener(Event.ENTER_FRAME,fall);

//ball.parent.removeChild(ball); <- you probably don't want to remove the ball from the display

}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines