-
Collision with an object replacing the mouse
Hi,
I am a beginner to Actionscript 3
I am creating a simple game (for practice purpose)
here is my code:
Code:
stop();
var lvl1:MovieClip = new Linelvl1();
lvl1.x = 400;
lvl1.y = 240;
addChild(lvl1);
lvl1.alpha = 0;
var mouse:MovieClip = new Ball();
mouse.x = 400;
mouse.y = 240;
addChild(mouse);
addEventListener(Event.ENTER_FRAME, onStart);
function onStart(evt:Event) {
if (lvl1.hitTestPoint(mouseX, mouseY, true)) {
removeEventListener(Event.ENTER_FRAME, onStart);
removeChild(lvl1);
gotoAndStop("main");
}
}
mouse.x = mouseX;
mouse.y = mouseY;
Mouse.hide();
}
So now, my question is this.
As you've noticed I created a "Ball" that replaces the mouse and I've hid the mouse. Whenever the mouse or the "Ball" touches "lvl1" I want it to go to "main". But it doesn't work
I even changed hitTestPoint to hitTestObject but....I don't know why it doesn't work
Any help...?
-
Dignitary
one to many }
PHP Code:
stop();
var lvl1:MovieClip = new Linelvl1();
lvl1.x = 400;
lvl1.y = 240;
addChild(lvl1);
lvl1.alpha = 0;
var mouse:MovieClip = new Ball();
mouse.x = 400;
mouse.y = 240;
addChild(mouse);
addEventListener(Event.ENTER_FRAME, onStart);
function onStart(evt:Event) {
if (lvl1.hitTestPoint(mouseX, mouseY, true)) {
removeEventListener(Event.ENTER_FRAME, onStart);
removeChild(lvl1);
gotoAndStop("main");
}
mouse.x = mouseX;
mouse.y = mouseY;
Mouse.hide();
}
worked for me
-
Thank you
But it still doesn't work for me
Maybe it's because I didn't put the whole code
That's the whole code
PHP Code:
stop();
var lvl1:MovieClip = new Linelvl1();
lvl1.x = 400;
lvl1.y = 240;
addChild(lvl1);
lvl1.alpha = 0;
addEventListener(Event.ENTER_FRAME, onStart);
function onStart(evt:Event) {
var myTimer:Timer = new Timer(1000, 1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (evt:TimerEvent) {
if (lvl1.alpha < .75) {
lvl1.alpha += .05;
}
}
myTimer.start();
if (lvl1.alpha == .75) {
if (lvl1.hitTestPoint(mouseX, mouseY, true)) {
removeEventListener(Event.ENTER_FRAME, onStart);
removeChild(lvl1);
gotoAndStop("main");
}
}
}
With this code as soon as my mouse touches "lvl1" it works
but I want to hide the mouse and replace the mouse with another object. as soon as THAT object hits "lvl1" I wanted to do a specific action.
Now what shall I add?
shall I use hitTestObject or hitTestPoint?
-
Actually here's the complete explanation
This is what it looks like
The black ball can be dragged. I want the ball to be able to travel between the hole. But if it touches the black coloured area, it has to go and play "main"
And this is my code
Actionscript Code:
stop();
var lvl1:MovieClip = new Linelvl1(); lvl1.x = 400; lvl1.y = 240; addChild(lvl1); lvl1.alpha = 0;
var ball:MovieClip = new Ball(); ball.x = 80; ball.y = 380; addChild(ball);
addEventListener(Event.ENTER_FRAME, onStart);
function onStart(evt:Event) { var myTimer:Timer = new Timer(1000, 1); myTimer.addEventListener(TimerEvent.TIMER, timerListener); function timerListener (evt:TimerEvent) { if (lvl1.alpha < .75) { lvl1.alpha += .05; } } myTimer.start(); ball.addEventListener(MouseEvent.MOUSE_DOWN, onDrag); ball.addEventListener(MouseEvent.MOUSE_UP, onStopDrag); function onDrag(evt:MouseEvent) { ball.startDrag(true); } function onStopDrag(evt:MouseEvent) { ball.stopDrag(); } if (lvl1.alpha == .75) { if (lvl1.hitTestObject(ball)) { removeEventListener(Event.ENTER_FRAME, onStart); removeChild(lvl1); removeChild(ball); gotoAndStop("main"); ball.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag); ball.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag); } } }
-
Dignitary
Clips are always squares for hitTest objects so you kinda have to make 3 separate walls unless you wanna use some of the fancier hitTest class files out there.
I'm not sure of the need for your objects to be class files in the library either. If you can, you should consider just drawing the object and convert to symbols. It would make the code rather simple:
PHP Code:
import flash.events.MouseEvent;
import flash.events.Event;
ball.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
ball.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
ball.addEventListener(Event.ENTER_FRAME, hitWalls);
function hitWalls(e:Event):void{
if(e.currentTarget.hitTestObject(wall1)||e.currentTarget.hitTestObject(wall2)||e.currentTarget.hitTestObject(wall3)){
e.currentTarget.stopDrag();
e.currentTarget.x=28;
e.currentTarget.y = 303;
}
}
function stopDragging(m:MouseEvent):void{
m.currentTarget.stopDrag();
}
function startDragging(m:MouseEvent):void{
m.currentTarget.startDrag();
}
-
Originally Posted by rynoe
Clips are always squares for hitTest objects so you kinda have to make 3 separate walls unless you wanna use some of the fancier hitTest class files out there.
I'm not sure of the need for your objects to be class files in the library either. If you can, you should consider just drawing the object and convert to symbols. It would make the code rather simple:
PHP Code:
import flash.events.MouseEvent;
import flash.events.Event;
ball.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
ball.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
ball.addEventListener(Event.ENTER_FRAME, hitWalls);
function hitWalls(e:Event):void{
if(e.currentTarget.hitTestObject(wall1)||e.currentTarget.hitTestObject(wall2)||e.currentTarget.hitTestObject(wall3)){
e.currentTarget.stopDrag();
e.currentTarget.x=28;
e.currentTarget.y = 303;
}
}
function stopDragging(m:MouseEvent):void{
m.currentTarget.stopDrag();
}
function startDragging(m:MouseEvent):void{
m.currentTarget.startDrag();
}
Thank you for your help
I'm really trying to understand what you're saying but I can't
1. My wall is only one as you see it in the picture. How do you want me to create 3 walls
2. what are the x = 28 and y = 303 for??
\
thank you
-
I'm sorry please ignore my last post
I got it
Thank you
But now this is level 1
what if in future levels I would wanna put a let's say diagonal wall....??
then when I convert it to a symbol it'll be square.... (
-
Client Software Programmer
Actually it is possible to make pixel perfect hit tests if you use .as files check this free source code that has pixel perfect collisions and a free download: http://www.freeactionscript.com/cate...ion-detection/
-
Dignitary
You can also download the latest kit here.
This is one of the 'fancier' class files I mentioned in earlier post. If you are just starting AS3 this may seem a bit overwhelming though.
Last edited by rynoe; 07-18-2012 at 10:12 AM.
Reason: Explosive diarrhea
[SIGPIC][/SIGPIC]
-
Originally Posted by rynoe
You can also download the latest kit here.
This is one of the 'fancier' class files I mentioned in earlier post. If you are just starting AS3 this may seem a bit overwhelming though.
I've read the book ActionScript 3 for Beginners
It's an amazing book and they suggested this for pixel perfect also.
But no one ever explained how to use it in your own project
-
For example I want exactly something like this
but this is in AS2
I want it in AS3
http://gskinner.com/blog/archives/20...8_shape_b.html
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|