as3 dropTarget return instance not the name - actionscript

I was trying to put a sprite over another sprite and get the name of the drop target. When I use a loder in my sprite the result is not the name of the sprite, but his instance number. I will post the code below and hope that someone could help me. Thanks alot!
package
{
import flash.display.*;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.MouseEvent;
public class dragAndDrop extends MovieClip
{
public function dragAndDrop()
{
// constructor code
var imagineDrag:Sprite = new Sprite;
imagineDrag.x = 150;
imagineDrag.y = 150;
var fundalLoader:Loader = new Loader();
var fundalLoaderURL:URLRequest = new URLRequest("butStartActiv.png");
fundalLoader.load(fundalLoaderURL);
imagineDrag.addChild(fundalLoader);
imagineDrag.name = "Tinta";
addChild(imagineDrag);
var target1:Sprite = new Sprite();
target1.graphics.beginFill(0xCCFF00);
target1.graphics.drawRect(265, 100, 125, 125);
target1.name = "casuta1";
addChild(target1);
var imagineDeTras:Sprite = new Sprite;
imagineDeTras.x = 10;
imagineDeTras.y = 10;
var fundalLoader2:Loader = new Loader();
var fundalLoaderURL2:URLRequest = new URLRequest("butStartInactiv.png");
fundalLoader2.load(fundalLoaderURL2);
imagineDeTras.addChild(fundalLoader2);
addChild(imagineDeTras);
imagineDeTras.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
imagineDeTras.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
imagineDeTras.buttonMode = true;
imagineDeTras.useHandCursor = true;
imagineDeTras.mouseChildren = false;
function dragObject(evt:MouseEvent):void
{
evt.currentTarget.startDrag();
trace("nume : " + evt.currentTarget.name)
}///// end drag object
function stopDragObject(evt:MouseEvent):void
{
//trace("e.target.name " + e.target.name);
trace("tinta atinsa este: " + evt.target.dropTarget.name);
evt.target.stopDrag();
}//// end function stop drag
}/// end constructor
}// end class
}
When you drag and drop imagineDeTras over target1, the answer is "casuta1"
When I drag and drop imagineDeTras over imagineDrag, instead of "Tinta" the answer is instance126. Can somebody help me with this problem? Thank you very much!

The problem is a tiny mistake on your code!
On dragObject function you wrote :
trace("nume : " + evt.**currentTarget**.name)
And it's correct, But you forgot to use currentTarget instead of target on the stopDragObject!
trace("tinta atinsa este: " + evt.**target**.dropTarget.name);
You have to replace the target with currentTarget to make it trace the name of item that you add your listener to it instead of the name of it's child that mouse_up event triggered on.
Also you have to remove "dropTarge" after the curretlntTarget
You can see this if you have problem on understanding target and currentTarget: AS3: Difference between target vs currentTarget

Related

Vaadin 14 paper-slider pin property

I am still new in Vaadin and I am trying to implement #polymenr/paper-slider component using Vaadin 14.
I have downloaded the project https://github.com/berndhopp/vaadin-paper-sliders from Github and I am trying to set "pin" property as explained here
https://vaadin.com/forum/thread/18215247/vaadin-14-slider
In PaperSlider.java
...
public void setPin(boolean pin) {
this.getElement().setProperty("pin", pin);
}
...
In DemoView.java
package org.vaadin.addon.sliders.ui;
//import org.vaadin.addon.sliders.PaperSlider;
import org.vaadin.addon.sliders.PaperSlider;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
#Route("")
public class DemoView extends VerticalLayout
{
private PaperSlider slider = null;
private Label sliderValue = null;
public DemoView()
{
Anchor sourceLink = new Anchor("https://github.com/markhm/vaadin-paper-sliders", " (source code on GitHub)");
sourceLink.setTarget("_blank");
HorizontalLayout titleBox = new HorizontalLayout();
titleBox.setAlignItems(Alignment.BASELINE);
H3 title = new H3("Vaadin paper-slider, ported to Vaadin v14");
titleBox.add(title, sourceLink);
add(titleBox);
//
// https://stackoverflow.com/questions/68531460/vaadin-14-paper-slider-pin-property
//
HorizontalLayout sliderLine = new HorizontalLayout();
sliderLine.setHeight("100px");
Label sliderValue = new Label();
PaperSlider paperSlider = new PaperSlider(0,100,20);
paperSlider.addValueChangeListener(e -> sliderValue.setText("New Slider value: " + e.getValue()));
paperSlider.setPin(true);
sliderLine.add(paperSlider, sliderValue);
add(sliderLine);
// slider.getElement().getStyle().set("padding-top", "30px") ;
//Label whiteline = new Label("");
//whiteline.setHeight("50px");
//add(whiteline);
// Testing pin property
// HorizontalLayout mySliderLine = new HorizontalLayout();
//sliderValue = new Label("mySlider value");
//PaperSlider paperSlider = new PaperSlider(0,100,20);
//paperSlider.getElement().getStyle().set("padding-top", "30px");
//mySliderLine.setHeight("100px");
//paperSlider.setPin(true);
//mySliderLine.add(paperSlider);
// slider.setPin(true) ;
// add(slider) ;
// NB: The PaperRangeSlider does not support Polymer 3.
// HorizontalLayout rangeSliderLine = new HorizontalLayout();
//
// Label rangeValues = new Label("Range values");
// rangeSlider = new PaperRangeSlider(0, 100, 40, 60);
// rangeSlider.addValueChangeListener(e -> rangeValues.setText("Range values: " + e.getValue()));
// rangeSliderLine.add(rangeSlider, rangeValues);
// add(rangeSliderLine);
}
}
I can see the paper slider but I don't see the numeric value label when the slider thumb is pressed.
Can anyone help with this please
Thanks
Alex
It seems numeric value get lost within the layout. You can try adding height to the layout, something like sliderLine.setHeight("100px") or adding some padding to the component paperSlider.getElement().getStyle().set("padding-top", "30px").
from the original post I missed the part that you have an extra Label component and that's the value that you can't see. If I do this:
HorizontalLayout sliderLine = new HorizontalLayout();
sliderLine.setHeight("100px");
Label sliderValue = new Label();
PaperSlider paperSlider = new PaperSlider(0,100,20);
paperSlider.addValueChangeListener(e -> sliderValue.setText("New Slider value: " + e.getValue()));
paperSlider.setPin(true);
sliderLine.add(paperSlider, sliderValue);
add(sliderLine);
I get to see that label with the selected value

mystery Error #1063: Argument count mismatch

So I'm getting a "Error #1063: Argument count mismatch" error. The weird thing is that it isn't keeping the game from running, but I would like to know why I'm even getting an error in the first place. The full error is:
ArgumentError: Error #1063: Argument count mismatch on Hock(). Expected 3, got 0.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at PlayScreen()[Z:\PROJECTS\Silversound\Occulus Squish\Oculus Squish\Classes\PlayScreen.as:30]
at Main/addPlayscreen()[Z:\PROJECTS\Silversound\Occulus Squish\Oculus Squish\Classes\Main.as:17]
at Main()[Z:\PROJECTS\Silversound\Occulus Squish\Oculus Squish\Classes\Main.as:13]
at runtime::ContentPlayer/loadInitialContent()
at runtime::ContentPlayer/playRawContent()
at runtime::ContentPlayer/playContent()
at runtime::AppRunner/run()
at ADLAppEntry/run()
at global/runtime::ADLEntry()
The Code for PlayScreen is:
import flashx.textLayout.formats.BackgroundColor;
import flash.display.SimpleButton;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class PlayScreen extends MovieClip
{
public var batArmy:Array;
public var hockArmy:Array;
public var shadow:Shadow;
public var crossHairs:CrossHairs;
var Layer01:MovieClip;
var Layer02:MovieClip;
var Layer03:MovieClip;
var Layer04:MovieClip;
var Layer05:MovieClip;
var randomX:Number = 300 + (660 - 300) * Math.random();
public function PlayScreen()
{
//Mouse.hide();
addBatButton.addEventListener(MouseEvent.CLICK, addBat);
addHockButton.addEventListener(MouseEvent.CLICK, addHock);
batArmy = new Array();
hockArmy = new Array();
//addEventListener(Event.ENTER_FRAME, crossHairsMove);
//stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress );
Layer01 = new MovieClip;
this.addChild(Layer01);
Layer02 = new MovieClip;
this.addChild(Layer02);
Layer03 = new MovieClip;
this.addChild(Layer03);
Layer04 = new MovieClip;
this.addChild(Layer04);
Layer05 = new MovieClip;
this.addChild(Layer05);
//add crossHair
/*crossHairs = new CrossHairs(mouseX,mouseY,this);
Layer05.addChild (crossHairs);
addEventListener(Event.ENTER_FRAME, crossHairsMove);*/
}
/*public function onKeyPress( keyboardEvent:KeyboardEvent ):void
{
if ( keyboardEvent.keyCode == Keyboard.DOWN )
{
trace("yar");
addBat;
}
}*/
public function addBat( mouseEvent:MouseEvent ):void
{
var randomX:Number = 300 + (660 - 300) * Math.random();
var newBat = new Bat( randomX, -50, this);
batArmy.push ( newBat );
Layer02.addChild (newBat);
}
public function addHock( mouseEvent:MouseEvent ):void
{
var newHock = new Hock(-72, 170, this);
hockArmy.push ( newHock );
Layer02.addChild (newHock);
}
/*public function crossHairsMove ( e:Event ):void
{
crossHairs.x = mouseX;
crossHairs.y = mouseY;
}*/
}
and from the looks of it the error has something to do with the Hock class, so here's the code for that:
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.ui.Mouse;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Hock extends MovieClip
{
private var _screen: PlayScreen;
public var xSpeed:Number;
public function Hock( startX:Number, startY:Number, screen:PlayScreen )
{
_screen = screen;
x = startX;
y = startY;
width = 100;
scaleY = scaleX;
addEventListener(Event.ENTER_FRAME, moveRightFar);
addEventListener(Event.ENTER_FRAME, moveSpeed)
}
public function moveSpeed( e:Event ):void
{
x += xSpeed;
}
public function moveRightFar ( e:Event): void
{
if (x < 0)
{
gotoAndStop("rollRight");
xSpeed = 13;
}
else if (x >= 240)
{
gotoAndStop("still")
xSpeed = 0;
}
}
}
Now I could be wrong but I think it's having a problem with var newHock = new Hock(-72, 170, this); in the "addHock" function, but I have 3 arguments there, not 0. Right? Anyway, like I said, it's not keeping the game from running but it is kind of annoying, so any insight is welcome. I'm sure it's something obvious. Thanks!
I have a guess, but I'll explain how i got there first ...
the first line of the stacktrace pointing at your source code is
at PlayScreen()[Z:\PROJECTS\Silversound\Occulus Squish\Oculus Squish\Classes\PlayScreen.as:30]
which points at the first line of the constructor of PlayScreen: addBatButton.addEventListener(MouseEvent.CLICK, addBat);
but obviously the problem is not there ...
But PlayScreen extends MovieClip and since you didn't specifically included a super() statement, the compiler will put that at as the first command. In fact, the previous lines of the stack point to the constructor of MovieClip and then to a mysterious constructChildren() method of Sprite
That happens to be an internal method used to create the childs of the Sprite that you might have setup on your clip's stage directly from Animate.
So my guess is, the player is trying to instantiate a symbol that extends Hock and that you positioned on the stage somewhere, and of course is doing it by passing zero arguments because that's what a normal Sprite would expect.
Check your library to see what extends Hock and then see which one of those is placed in some other symbol's stage. Your options then will be to remove that and create it from code or to rework the class signature to take zero arguments.

how do i add style to a textfield in AS3?

When i press a button it will display text, but how would i add style? such as position and color.
import flash.text.TextField;
var tf:TextField= new TextField();
convob.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
tf.text="hello world";
addChild(tf);
}
Something like this:
var format:TextFormat = new TextFormat();
format.color = 0x555555;
format.size = 18;
format.align = TextFormatAlign.CENTER;
format.bold = false;
tf.setTextFormat(format);
Check class reference for TextFormat: http://help.adobe.com/en_GB/FlashPlatform/reference/actionscript/3/flash/text/TextFormat.html

How to draw a line on a bitmap in Stage3D using Agal?

How can I draw a line on a bitmap in Stage3D using Agal? Can someone provide a code example?
If you use Starling you can try this:
/**
* Class Line
* #author Leandro Barreto 2012
* #version 1.0
**/
package starling.utils
{
import starling.display.Quad;
import starling.display.Sprite;
public class Line extends Sprite
{
private var baseQuad:Quad;
private var _thickness:Number = 1;
private var _color:uint = 0x000000;
public function Line()
{
baseQuad = new Quad(1, _thickness, _color);
addChild(baseQuad);
}
public function lineTo(toX:int, toY:int):void
{
baseQuad.width = Math.round(Math.sqrt((toX*toX)+(toY*toY)));
baseQuad.rotation = Math.atan2(toY, toX);
}
public function set thickness(t:Number):void
{
var currentRotation:Number = baseQuad.rotation;
baseQuad.rotation = 0;
baseQuad.height = _thickness = t;
baseQuad.rotation = currentRotation;
}
public function get thickness():Number
{
return _thickness;
}
public function set color(c:uint):void
{
baseQuad.color = _color = c;
}
public function get color():uint
{
return _color;
}
}
}
Someone suggested at the Starling forums that we create a Line class which draws a few quads connecting two points. This tutorial shows how to create polygons using AGAL for shaders:
http://wiki.starling-framework.org/manual/custom_display_objects
I recently wrote a simple library to draw lines on Stage3D.
It's called Zebroid, https://github.com/luwes/Zebroid
Zebroid does not support line caps or joints yet.

AS2: Access class function from onRollOver

I am working on a class for building drop down buttons dynamically. Here is excerpt one of my code (located in the Class constructor):
_button.onRollOver = function()
{
this.gotoAndStop("over");
TweenLite.to(this.options,0.2 * optionCount,{_y:mask._y, ease:Strong.easeOut, onComplete:detectMouse, onCompleteParams:[button]});
function detectMouse(button:MovieClip)
{
button.options.onMouseMove = function()
{
for (var option:String in this._parent.children)
{
if (this._parent.children[option].hitTest(_root._xmouse, _root._ymouse, true))
{
if (!this._parent.children[option].active) {
this._parent.children[option].clear();
drawOption(this._parent.children[option], "hover");
this._parent.children[option].active = true;
}
}
}
};
}
};
I am attempting to call on the function drawOption() which is inside the same class and looks like so:
private function drawOption(option:MovieClip, state:String)
{
trace("yo");
switch (state)
{
case "hover" :
var backgroundColour:Number = _shadow;
var textColour:Number = 0xffffff;
break;
default :
var backgroundColour:Number = _background;
var textColour:Number = _shadow;
break;
}
option._x = edgePadding;
option._y = 1 + edgePadding + (optionPadding * (option.index)) + (optionHeight * option.index);
option.beginFill(backgroundColour,100);
option.lineStyle(1,_border,100,true);
option.moveTo(0,0);
option.lineTo(_optionWidth,0);
option.lineTo(_optionWidth,optionHeight);
option.lineTo(0,optionHeight);
option.endFill();
var textfield:TextField = option.createTextField("string", option.getNextHighestDepth(), 20, 2, _optionWidth, optionHeight);
var format:TextFormat = new TextFormat();
format.bold = true;
format.size = fontSize;
format.font = "Arial";
format.color = textColour;
textfield.text = option.string;
textfield.setTextFormat(format);
}
But because I am trying to call from inside an onRollOver it seems that it is unable to recognise the Class methods. How would I go about accessing the function without making a duplicate of it (very messy, do not want!).
In AS2 I prefer to use the Delegate class to add functions to event handlers whilst maintaining control over the scope.
You implement it like this:
import mx.utils.Delegate;
//create method allows you to set the active scope, and a handler function
_button.onRollOver = Delegate.create(this,rollOverHandler);
function rollOverHander() {
// since the scope has shifted you need to use
// the instance name of the button
_button.gotoAndStop("over");
TweenLite.to(_button.options,0.2 * optionCount,{_y:mask._y, ease:Strong.easeOut, onComplete:detectMouse, onCompleteParams:[button]});
}
everything in the onrollover relates to the button which is rolled over, to access the outer functions, you would have to navigate to the outer class before calling the function in exactly the same way that you are accessing the outer variables, eg:
if the parent of the button contains the function:
this._parent.drawOption(....)
ContainerMC class:
class ContainerMC extends MovieClip{
function ContainerMC() {
// constructor code
trace("Container => Constructor Called");
}
function Init(){
trace("Container => Init Called");
this["button_mc"].onRollOver = function(){
trace(this._parent.SayHello());
}
}
function SayHello():String{
trace("Container => SayHello Called");
return "Hellooooo World";
}
}
I then have a movieclip in the library with the Class ContainerMC and the identitfier Container_mc, which is added to the stage by this line in the main timeline:
var Container = attachMovie("Container_mc","Container_mc",_root.getNextHighestDepth());
Container.Init();
Edit: added working sample

Resources