How to drag a symbol only horizontally? - actionscript-3

I'm currently trying to make a pretty simple drag and drop Flash program.
I made a program in which you can drag a square with your mouse.
However I would like the square only to move horizontally. I've been trying to find something on the internet pretty long now, without a finding a solution. So I thought maybe you guys could help me...
Here's what I've done:
I first made a square and made a symbol of it named: "blok"
Then I wrote the following code within the same scene:
var myblock:Sprite = blok;
this.addChild(myblock);
myblock.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent):void
{
myblock.startDrag();
}
myblock.addEventListener(MouseEvent.MOUSE_UP, stopMove);
function stopMove(e:MouseEvent):void
{
myblock.stopDrag();
}

startDrag takes a bounding box parameter. Try this:
function startMove(evt:MouseEvent):void
{
myblock.startDrag(false, new Rectangle(0, myblock.y, 1000, myblock.y));
}
The 0 and 1000 are min and max x values, substitute whatever you need to use in your application.
Adobe documentation for Sprite class

Related

How can i get a function to use any movie clip?

I'm a little lost in understanding this kind of function, i get the feeling this has been asked a thousand times but cannot find an explanation of what the code is doing.
Basically i just want a movie clip with instance name box to do something, then reuse the function for other movie clips afterwards
a little like this, but working.
Many Thanks
//my function to be used on "instance name" box
myfunc (box);
function myfunc ();
{
while (this is happening);
{
//in this case box.x = goes where ever i put it
.x = goes here
.y = goes here
}
}
Sorry it's not quite English, my communication skills are terrible
Sure you can do that. You give the function a parameter, then refer to a parameter to change its properties. With such a simple movement function it could accept a DisplayObject - a distant superclass of a MovieClip and thus a superclass to many other possible classes of those objects that can be displayed by Flash.
function myfunc(param:DisplayObject):void {
// no semicolon after declaring the function!
while (somethingIsHappening(param)) {
// why not call a query on that object?
param.x+=1; // move right by 1 pixel
}
}
You might want to look at this manual on ActionScript 3 syntax, and the following pages on variables, functions and classes, to learn more.

Actionscript3 Simple platform collision confusion

I'm making a 2D platformer game and i'm trying to add collisions to the platforms so that when the character hits it it cannot pass through. I'm struggling to find the syntax to use to create this collision. So far this is what I have.
Also i would still like to be able to use hitTestObject within the if statement.
thanks
public function platform1Collision():void
{
if (fireboy1.hitTestObject(Platform1))
{
//fireboy1 cannot pass through
}
}
You'll probably want to prevent fireboy1's y property from extending past Platform1's y property:
function platform1Collision():void
{
if(fireboy1.hitTestObject(Platform1))
{
if(fireboy1.y > Platform1.y)
{
fireboy1.y = Platform1.y + Platform1.height;
}
else
{
fireboy1.y = Platform1.y - fireboy1.height;
}
}
}
NOTE: The above code sample assumes top-left orientation for both fireboy1 and Platform1.
EDITED: The above edited code will allow fireboy1 to walk beneath Platform1, but not pass through it.
This is a very rudimentary example to give you an idea of the type of logic you can use. If you want to allow fireboy1 to pass through Platform1 from below, you'll have to update the logic to allow for that. For example, if you take out the if/else and just automatically place fireboy1 above Platform1 every time they collide, it will appear as if player1 is jumping onto Platform1 when it is approached from below.

Box2d MovieClip to original position

I want to try a simple task where if i move a object inside the world and then press a button it should go back to its original position , but its not working , below is the code i am using - the file is here - http://www.fastswf.com/yAnIvBs (when i remove the event listener)
with event listener - http://www.fastswf.com/rpYsIt8
////////========================
stop();
var startXPos:Number = level1WorldObj.box1.x;
var startYPos:Number = level1WorldObj.box1.y;
function areaS(e:Event) {
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
level1WorldObj.box1.removeEventListener(Event.ENTER_FRAME, areaS);
}
but1.addEventListener(MouseEvent.CLICK,nClick3);
function nClick3(event:MouseEvent):void{
level1WorldObj.box1.addEventListener(Event.ENTER_FRAME, areaS);
level1WorldObj.box1.y= startYPos;
level1WorldObj.box1.x= startXPos;
}
/////////////////======================
Now i want to be able to do it many time so i kept the variables that detect the initial x, y as global ...
Here you can see how it behaves in debugdraw mode , strangely only the clip moves not the actual body - http://www.fastswf.com/-Ijkta4
Can some one please guide me here ...
Thanks in advance ...
Jin
The graphics that you see (box1) aren't related to the physical object behind the scenes - you're currently only moving the graphics not the object itself.
You need to use either SetPosition() or SetTransform() on the b2Body of the object
Edit 07/7
As you're using the Box2D World Construction Kit, I took a look at the source code (available here: https://github.com/jesses/wck). The main class seems to be BodyShape (https://raw.githubusercontent.com/jesses/wck/master/wck/BodyShape.as).
Looking through it, you should be able to access the b2Body directly. If it's null (which is probably the source of the TypeError that you're getting, then you haven't called createBody(), which is what actually takes all of your properties as creates the physical object behind the scenes.
Once you have a b2Body, if you want to position it based on the graphics, there's a function syncTransform() to do just that.
You should turn on debugDraw on your World class to make it easier to see what's going on in the background. NOTE: this needs to be done before calling create()
I was able to find solution to this problem , i found the starting point by using this -
trace(level1WorldObj.box1.b2body.GetPosition().x);
trace(level1WorldObj.box1.b2body.GetPosition().y);
then once i had the position manually i took down the coordinates and used the below code ....
level1WorldObj.box1.b2body.SetTransform(new V2(-2, 2),0 );
Thanks #divillysausages for all the help ...
Regards

Flash AS3 Creating components on stage via Button

Sorry for my complicated questioning ;)
I've created several components on my stage (Slider, TextLabel, CheckBox, ...) with the addChild(...) method. Now, if the user needs one Slider component more (for another input), I want to give him a button "addButton" (already on stage) with an CLICK addEventListener. The function which is fired in the addEventListener should create a new Slider component on stage, e.g.
addButton.addEventListener(MouseEvent.CLICK,addSlider("slid1",20,160,250,0,250,30));
The problem is to pass the parameters to the function, i need to set the new Slider to several basic values, like position, width, minimum, maximum and so on....
Can this be achieved in this way?
thx,
edwin
There are several ways you can do this. The simplest way is to call your function inside a mouse event handler:
addButton.addEventListener(MouseEvent.CLICK, addSliderHandler);
function addSliderHandler(e:MouseEvent):void {
{
addSlider("slid1",20,160,250,0,250,30);
}
EDIT (to answer your comment question):
To position your new component according to existing components, you can keep a variable reference to the position in the script and pass this as a parameter. For instance, suppose you want the first generated slider to be at y:160, and every slider to be 40px more than this, change the script to this:
var sliderY:int = 160;
addButton.addEventListener(MouseEvent.CLICK, addSliderHandler);
function addSliderHandler(e:MouseEvent):void {
{
addSlider("slid1",20,sliderY,250,0,250,30);//Pass sliderY instead of literal number
sliderY += 40;//Add 40 to sliderY
}
Now, every time you use this function to create a button it will be positioned at y = sliderY, then it will add 40 to sliderY....so the first one generated will be at 160, the next at 200, the next at 240 etc.
Hope this helps!

Frame labels & function parameters

Is it possible to change a frame label within a gotoAndStop('label') with the parameters in a function?
I'm playing around with updating code as I learn more and more techniques, and at the moment the code is a basic click-a-button to select the object shape, and on press the button disappears:
// Change the object into a circle.
circle_btn.addEventListener(MouseEvent.CLICK,function(){changeShape_fun(circle_btn,circle);});
// Change the object into a square.
square_btn.addEventListener(MouseEvent.CLICK,function(){changeShape_fun(square_btn,square);});
// Change the object into a star.
star_btn.addEventListener(MouseEvent.CLICK,function(){changeShape_fun(star_btn,star);});
function changeShape_fun(shape_btn,frame){
shape_btn.visible = false;
main_mc.gotoAndStop('frame');
}
However I can't/don't seem to know how to change a frame label through function parameters, or if what I'm trying to do is even possible.
Also to note, while I'm all ears for any more efficient ways of doing what I'm trying to do, I would still like to know how/if you can change frame labels through function parmeters.
Thanks! :)
You're very close, but you're trying to go to a frame called 'frame' and not the string contained within the frame variable. Try this instead:
// Change the object into a circle.
circle_btn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){changeShape_fun(circle_btn, 'circle');});
// Change the object into a square.
square_btn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){changeShape_fun(square_btn, 'square');});
// Change the object into a star.
star_btn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent){changeShape_fun(star_btn, 'star');});
function changeShape_fun(shape_btn,frame){
shape_btn.visible = false;
main_mc.gotoAndStop(frame);
}
This will go to a frame within main_mc called 'circle' when you click the circle or 'square' if you click the square, etc.