I'm an actionscript n00b, please excuse my ignorance.
currently trying to make my first person shooter game and have picked up great tips from Lynda(.com) but im having difficulty changing my mouse into a crosshair cursor and having that crosshair not show up permanently in subsequent frames as I create this game.
so i have this code on an action layer but on other layers when I create another key frame to change what I shoot at on other layers, my cursor stamps a permanent image of itself onto the screen. I definitely need to understand this language more if Im going to become good at it but for now i'f like to be able to make a few games to entertain myself, any suggestions? are appreciated
var cursor:MovieClip;
function initializeGame():void
{
cursor = new Cursor();
addChild(cursor);
cursor.enabled = false;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
}
function dragCursor(event:MouseEvent):void
{
cursor.x = this.mouseX;
cursor.y = this.mouseY;
}
initializeGame();
In AS3, items will remain on the display list (on screen) until explicitly removed (or their parent is removed).
So to keep your cursor from staying on screen, you need to remove it:
removeChild(cursor);
Doing Mouse.show() doesn't affect any display objects on the screen.
If you assign a new value to the var cursor, it doesn't automatically remove the previous object - as variables are just references to objects. So if in your subsequent frames you are making a new cursor, you want to remove the existing one prior to assigning a new one to the variable:
if(cursor.parent) removeChild(cursor); //if there is an object assigned to cursor, and it has a parent (which means it's on screen), then remove it before doing the below code
cursor = new Cursor();
addChild(cursor);
Related
I have an MC that is a list and it's inside a scrollPane. I want to make the list jump to a new location by clicking an object inside the MC.
I do not want the user to manually scroll the list. However, I'm not sure how to reference the MC in the scrollPane.
I'm lost on the coding, have some incredibly basic AS3 going on that doesn't work:
scrollPane.source = List1;
scrollPane.setSize(440, 610);
scrollPane.scrollDrag = true;
The list works in that I can manually scroll through it. So I tried this code to see if I coul move the MC inside the scrollPane up 300 pixels--it obviously didn't work:
button1.addEventListener(MouseEvent.MOUSE_DOWN, moveList1);
function moveList1(evt:MouseEvent):void {
scrollPane.source.List1.y = -300;
}
I'm assuming since the MC "List1" isn't actually on the stage is where I'm getting tripped up.
Thanks for any help.
Apparently I asked a similar question a long time ago that I since forgot about.
The solution is:
"scrollPane instance name".verticalScrollPosition = "wherever you want the pane to scroll to".
Example usage code:
scrollPane.verticalScrollPosition = 100;
I have made a game using Flash CS5 (as3) and I am trying to add a child to the stage behind objects that are already there. For example, I have a bar at the bottom of the screen that is there from the time you start the game and I have falling objects that I want to fall behind it, but instead they fall in front of it because I added them to the stage after. Is there a way of adding the falling objects behind it without having to keep re-adding the bar to the stage? Thanks in advance.
Rather than layering, I'd use adjust the index of each object using addChildAt and setChildIndex.
The following line adds your falling object behind every other DisplayObject on the stage (in this case, you should probably add your bar to the stage first)
stage.addChildAt(fallingObject, 0);
You can create Sprites to act as layers, and add the different objects to them. Here is an example that adds a layer for adding whatever you want behind the bar layer, and THEN adds the bar layer, so it will be on top. It's super rough since you don't have any code to reference:
package {
import flash.display.*;
public class Test extends MovieClip {
var barLayer:Sprite;
var objectLayer:Sprite;
public function Test() {
var objectLayer = new Sprite();
//add the object layer to your main Class
this.addChild(objectLayer);
//now you can add movie clips or sprites to objectLayer whenever you like
//then create the bar layer
var barLayer = new Sprite();
//add your bar Display Object here
var bar = new MovieClip();//draw the bar or reference the library object you've created
barLayer.addChild(bar);
//now add the barLayer
this.addChild(barLayer);
}
}
}
I have a custom cursor in my flash project. That cursor consists of several parts (i.e. several movie clips inside of the cursor movie clip). And when the cursor rolls over different movie clips in my project, the parts of the cursor distort a little bit as though moving 1 pixel in relation to each other. And therefore the look of the whole cursor distorts a little. This happens every time the cursor crosses the boundary between the movie clips of the project (buttons, design pieces and so on). How can I make my cursor always retain one and the same look?
Thanks in advance
I'm guessing you're doing something like this to position your custom cursor:
stage.addEventListener(MouseEvent.MOUSE_MOVE, moved);
function moved(e:MouseEvent):void {
customCursor.x = e.stageX;
customCursor.y = e.stageY;
}
If so, when you move the mouse over a MovieClip or other element, your listener is receiving the event from that DisplayObject rather than the Stage. For some reason, DisplayObjects positioned at sub pixel values produce e.stageX and e.stageY values which aren't exactly the same as stage.mouseX and stage.mouseY, so your custom cursor elements are jumping slightly as the pixel values round differently.
Try using the Stage mouse position directly instead:
stage.addEventListener(MouseEvent.MOUSE_MOVE, moved);
function moved(e:MouseEvent):void {
customCursor.x = stage.mouseX;
customCursor.y = stage.mouseY;
}
I have created a map of Europe in Flash using AS3. I am an absolute beginner. Each country has been converted into a movie (using lasso tool). These movies are on one layer (with slight outline visible) on main timeline. On the other main timeline layer is the original outline map of Europe.
I have managed to make it so when a country is rolled over, a box tweens in scale to full size (to later insert text in).
I need to make it so that when each country is rolled over, that country's text box (which appears on Roll_Over) comes 'to the front'.
At the moment a couple of countries' text boxes work perfectly (ie: filled with white and completely visible), but most are either buried beneath the main map, or threading randomly through the faint movie clip outlines (which are at the front).
I'm guessing that this has to do with the display list. The countries that work perfectly are probably at the top. But how do I make it so that the country rolled over immediately goes to the top and returns on Roll_Out?
This would complete my map so any help would be VERY MUCH appreciated.
I have used following code:
//FRAME ONE
this.stop();
movieClip_6.addEventListener(MouseEvent.ROLL_OVER, fl_MouseOverHandler_15);
function fl_MouseOverHandler_15(event:MouseEvent):void
{
gotoAndPlay(2);
}
movieClip_6.addEventListener(MouseEvent.ROLL_OUT, fl_MouseOutHandler_67);
function fl_MouseOutHandler_67(event:MouseEvent):void
{
gotoAndStop(1);
}
//SECTION BELOW IS MY PROBLEM
movieclip_6.addEventListener(MouseEvent.ROLL_OVER,Rollover,false,0,true);
function Rollover(event:MouseEvent): void
{
setChildIndex(MovieClip(e.target),this.numChildren-1);
}
Many thanks in advance.
You can try calling addChild() again to the DisplayObject/Movieclip to bring it to the front. Instead of:
setChildIndex(MovieClip(e.target),this.numChildren-1);
Try going:
addChild(MovieClip(e.target));
You can use "layers" to bring objects in front of another.
constructor:
//Create the front layer
var frontLayer:Sprite = new Sprite();
//put all text boxes into front layer
frontLayer.addChild(textbox1); //etc...
//Add layer to mc
addChild(frontLayer);
rollover pops up:
textbox1.visible = true;
pop down:
textbox1.visible = false;
Pretty basic question here, but its still got me a little confused..
I have an object(navigation menu bar) that I want to change the colors on with code, so in an updateColor function, I get the bounds of the object (which is a drawing shape contained in a movieclip) and redraw a new shape on top of it with the new color, but I've noticed that the last shape still exists behind this redraw.
I tried using obj.graphics.clear(); before the redraw but that didn't get rid of the original shape. Is there another command that I'm overlooking?
Unless you drew the object you wish to remove within the same graphics object, clearing won't work. You need to remove the DisplayObject.
Depending on the number of children you can do:
obj.removeChildAt(0);
This also removes movieclips / buttons you placed on the stage manually.
If you have a reference to the DisplayObject you wish to remove you can simply do
obj.removeChild(backgroundClip);
Note that you can also change the color of a DisplayObject directly:
import flash.geom.ColorTransform;
...
public var test:MovieClip; //instance on stage
...
var cf:ColorTransform = test.transform.colorTransform;
cf.color = 0xff0000;
test.transform.colorTransform = cf;
while(this.numChildren)
{
this.removeChildAt(0);
}
Will clear child object on this MovieClip,
if it's clearing too much, then put the shape drawing in a subclip, and clear the subclip.