how to change IViewport verticalScrollPosition dynamicly - flex4

I'm trying to scroll VGroup with scroller:
<s:Scroller id="scroller" width="100" height="100">
<s:VGroup id="vp" width="100%" height="100%">
<my:TripView id="one"/>
<my:TripView id="two"/>
// if any more TripView.....
</s:VGroup>
</s:Scroller>
The TripView is dynamiclly generated, so the VGroup contentHeight may much greater than viewPortHeight. Since I could drag stuff in the TripView, I want to change the vp.verticalScrollPosition when the drag stuff almost move to the bottom of the view so the other TripView could be in the screen.

The most important thing is the scrollRect. Change the verticalScrollPosition based on the scrollRect. To use the ScrollManager, you should register viewport by registerViewport function. Then listen for mouseMove event, in the event listener invoke startScroll. Following is implementation:
package utils {
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import mx.core.IInvalidating;
import spark.components.Group;
public class ScrollManager {
private var viewport:Group;
private var stage:Stage;
private var oldMovingMouseY:Number;
private static var _instance:ScrollManager;
//threshold for scrolling
private const FUDGE:Number = 35;
//scroll up speed
private const UP_SCROLL_DELTA:int = 50;
//scroll down speed
private const DOWN_SCROLL_DELTA:int = 80;
public function registerViewport(viewport:Group):void {
this.viewport = viewport;
this.stage = viewport.stage;
}
public static function getInstance():ScrollManager {
if(_instance == null) {
_instance = new ScrollManager();
}
return _instance;
}
public function startScroll(mouseEvent:MouseEvent):void {
oldMovingMouseY = mouseEvent.stageY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 0, true);
}
private function onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
if (viewport is IInvalidating) {
//viewport.callLater(IInvalidating(viewport).validateNow);
//change VSP on scrollRect.y
var rect:Rectangle = viewport.scrollRect;
viewport.verticalScrollPosition = rect.y;
trace("========final vsp ========", rect.y);
}
}
private function onMouseMove(event:MouseEvent):void {
var currentMouseX:Number = event.stageX;
var currentMouseY:Number = event.stageY;
trace("mouseY ", currentMouseY);
//scroll direction
var delta:Number = currentMouseY - oldMovingMouseY;
var direction:int = (delta > 0) ? 1 : (delta < 0) ? -1: 0;
var scrollDelta:Number = direction > 0 ? DOWN_SCROLL_DELTA : UP_SCROLL_DELTA;
//current mousePoint in viewport coordination
var localPoint:Point = viewport.globalToLocal(new Point(currentMouseX, currentMouseY));
trace("localPoint: ", localPoint);
var scrollRect:Rectangle = viewport.scrollRect;
trace("viewport rect", scrollRect);
//determine if need scroll
if(needScroll(localPoint, scrollRect, direction)) {
trace("direction ", direction > 0 ? " UP": " DOWN");
scrollRect.y += scrollDelta*direction;
viewport.scrollRect = scrollRect;
if (viewport is IInvalidating) {
IInvalidating(viewport).validateNow();
}
}
oldMovingMouseY = currentMouseY;
}
private function needScroll(localPoint:Point, scrollRect:Rectangle, direction:int):Boolean {
var localY:Number = localPoint.y;
var bottom:Number = scrollRect.bottom;
var top:Number = scrollRect.top;
if(direction > 0 && (localY + FUDGE) > bottom) {
return true;
}
if(direction< 0 && (localY - FUDGE) < top) {
return true;
}
return false;
}
}
}

Related

html/css-/js drag and drop for mouse and touch combine with rotate with every click?

I use a script for moving an object both with mouse and touch screen. This works.
I have another script to make the object rotate 90 degrees with each click, this also works.
It just doesn't work together.
When I merge them, the move still works, but with a click the object moves back to the starting position and only rotates there.
I've read that I should merge this but I don't know how. Below both scripts.
Thanks in advance for responses.
var container = document.querySelector("#container");
var activeItem = null;
var active = false;
container.addEventListener("touchstart", dragStart, false);
container.addEventListener("touchend", dragEnd, false);
container.addEventListener("touchmove", drag, false);
container.addEventListener("mousedown", dragStart, false);
container.addEventListener("mouseup", dragEnd, false);
container.addEventListener("mousemove", drag, false);
function dragStart(e) {
if (e.target !== e.currentTarget) {
active = true;
// this is the item we are interacting with
activeItem = e.target;
if (activeItem !== null) {
if (!activeItem.xOffset) {
activeItem.xOffset = 0;
}
if (!activeItem.yOffset) {
activeItem.yOffset = 0;
}
if (e.type === "touchstart") {
activeItem.initialX = e.touches[0].clientX - activeItem.xOffset;
activeItem.initialY = e.touches[0].clientY - activeItem.yOffset;
} else {
console.log("doing something!");
activeItem.initialX = e.clientX - activeItem.xOffset;
activeItem.initialY = e.clientY - activeItem.yOffset;
}
}
}
}
function dragEnd(e) {
if (activeItem !== null) {
activeItem.initialX = activeItem.currentX;
activeItem.initialY = activeItem.currentY;
}
active = false;
activeItem = null;
}
function drag(e) {
if (active) {
if (e.type === "touchmove") {
e.preventDefault();
activeItem.currentX = e.touches[0].clientX - activeItem.initialX;
activeItem.currentY = e.touches[0].clientY - activeItem.initialY;
} else {
activeItem.currentX = e.clientX - activeItem.initialX;
activeItem.currentY = e.clientY - activeItem.initialY;
}
activeItem.xOffset = activeItem.currentX;
activeItem.yOffset = activeItem.currentY;
setTranslate(activeItem.currentX, activeItem.currentY, activeItem);
}
}
function setTranslate(xPos, yPos, el) {
el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
}
// Make blocks rotate 90 deg on each click
var count=0;
function rot(e){
count++;
var deg=count*90;
e.style.transform = "rotate("+deg+"deg)";
}
So I put them in a file but then it only works as described above.

how do i make FlxAsyncLoop work for making a progress bar in HaxeFlixel?

so, i tried to make an async loop for my game (to make a progress bar) and the game crashes when the state loads...
i tried to change the loops position so they don't collide and all of the code is from the FlxAsyncLoop demo but with other variables and some other changes.
here's the code:
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.addons.util.FlxAsyncLoop;
import flixel.group.FlxGroup;
import flixel.math.FlxPoint;
import flixel.math.FlxVelocity;
import flixel.text.FlxText;
import flixel.ui.FlxBar;
import flixel.util.FlxColor;
class PlayState extends FlxState
{
public static var player:FlxSprite;
var ground:FlxSprite;
var axe:FlxSprite;
var wood:FlxSprite;
var base:FlxSprite;
var txt:FlxText;
var playerPos:FlxPoint;
var enemy:FlxSprite;
var move = false;
var progressR:FlxGroup;
var progressE:FlxGroup;
var resourceGroup:FlxGroup;
var enemyGroup:FlxGroup;
var maxItems:Int = 100;
var bar1:FlxBar;
var bartxt1:FlxText;
var bar2:FlxBar;
var bartxt2:FlxText;
var loopR:FlxAsyncLoop;
var loopE:FlxAsyncLoop;
public function addR()
{
var wood = new FlxSprite(FlxG.random.int(100, 2500), FlxG.random.int(100, 1800));
wood.makeGraphic(40, 100, FlxColor.BROWN);
add(wood);
bar1.value = (resourceGroup.members.length / maxItems) * 100;
bartxt1.text = "Loading resources... " + resourceGroup.members.length + " / " + maxItems;
bartxt1.screenCenter();
}
public function addE()
{
var enemy = new FlxSprite(FlxG.random.int(300, 2500), FlxG.random.int(300, 1800));
enemy.makeGraphic(32, 32, FlxColor.RED);
add(enemy);
bar2.value = (enemyGroup.members.length / maxItems) * 100;
bartxt2.text = "Loading enemys... " + enemyGroup.members.length + " / " + maxItems;
bartxt2.screenCenter();
}
override public function create()
{
super.create();
resourceGroup = new FlxGroup(maxItems);
enemyGroup = new FlxGroup(maxItems);
loopR = new FlxAsyncLoop(maxItems, addR);
loopE = new FlxAsyncLoop(maxItems, addE);
FlxG.camera.zoom = 0.5;
playerPos = FlxPoint.get();
ground = new FlxSprite(0, 0);
ground.makeGraphic(2500, 1800, FlxColor.GREEN);
add(ground);
player = new FlxSprite(100, 100);
player.loadGraphic(AssetPaths.n1__png);
axe = new FlxSprite(player.x + 60, player.y);
axe.loadGraphic(AssetPaths.axeR__png);
camera = new FlxCamera(0, 0, 2500, 1800);
camera.bgColor = FlxColor.TRANSPARENT;
camera.setScrollBoundsRect(0, 0, ground.width, ground.height);
FlxG.cameras.reset(camera);
camera.target = player;
if (FlxG.collide(wood, player))
FlxObject.separate(wood, player);
if (FlxG.collide(wood, enemy))
FlxObject.separate(wood, enemy);
if (FlxG.collide(enemy, player))
FlxObject.separate(enemy, player);
progressR = new FlxGroup();
bar1 = new FlxBar(0, 0, LEFT_TO_RIGHT, FlxG.width, 50, null, "", 0, 100, true);
bar1.value = 0;
bar1.screenCenter();
progressR.add(bar1);
bartxt1 = new FlxText(0, 0, FlxG.width, "Loading resources... 0 / " + maxItems);
bartxt1.setFormat(null, 28, FlxColor.WHITE, CENTER, OUTLINE);
bartxt1.screenCenter();
progressR.add(bartxt1);
progressE = new FlxGroup();
bar2 = new FlxBar(0, 0, LEFT_TO_RIGHT, FlxG.width, 50, null, "", 0, 100, true);
bar2.value = 0;
bar2.screenCenter();
progressE.add(bar2);
bartxt2 = new FlxText(0, 0, FlxG.width, "Loading enemys... 0 / " + maxItems);
bartxt2.setFormat(null, 28, FlxColor.WHITE, CENTER, OUTLINE);
bartxt2.screenCenter();
progressE.add(bartxt2);
resourceGroup.visible = false;
resourceGroup.active = false;
enemyGroup.visible = false;
enemyGroup.active = false;
add(progressR);
add(progressE);
add(resourceGroup);
add(enemyGroup);
add(loopR);
}
override public function update(elapsed:Float)
{
super.update(elapsed);
if (FlxG.keys.pressed.LEFT && move)
{
player.x -= 5;
axe.loadGraphic(AssetPaths.axeL__png);
}
if (FlxG.keys.pressed.RIGHT && move)
{
player.x += 5;
axe.loadGraphic(AssetPaths.axeR__png);
}
if (FlxG.keys.pressed.UP && move)
{
player.y -= 5;
}
if (FlxG.keys.pressed.DOWN && move)
{
player.y += 5;
}
axe.x = player.x + 60;
axe.y = player.y;
playerPos = FlxPoint.get();
playerPos = player.getMidpoint();
FlxVelocity.moveTowardsPoint(enemy, playerPos, 70);
if (!loopR.started)
{
loopR.start();
}
else
{
if (loopR.finished)
{
resourceGroup.visible = true;
progressR.visible = false;
resourceGroup.active = true;
progressR.active = false;
loopR.kill();
loopR.destroy();
add(loopE);
loopE.start();
}
}
if (loopE.finished)
{
move = true;
add(player);
add(axe);
enemyGroup.visible = true;
progressE.visible = false;
enemyGroup.active = true;
progressE.active = false;
loopE.kill();
loopE.destroy();
}
}
}
i'm showing everything because of the functions and other things that can make this problem
(my english is bad sorry if i miss something...)
Without knowing the exact error, I suspect that the problem is when you call
if (FlxG.collide(wood, player))
FlxObject.separate(wood, player);
if (FlxG.collide(wood, enemy))
FlxObject.separate(wood, enemy);
if (FlxG.collide(enemy, player))
FlxObject.separate(enemy, player);
Inside your create function - wood and enemy are declared in other functions and do not exist there.
There are a couple of things I would do differently here:
(Note: I didn't TEST this code, so there may still be problems, but this might help get you pointed in the right direction...)
class PlayState extends FlxState
{
public static var player:FlxSprite;
var ground:FlxSprite;
var axe:FlxSprite;
var base:FlxSprite;
var txt:FlxText;
var playerPos:FlxPoint;
var move:Bool = false;
var progressR:FlxGroup;
var progressE:FlxGroup;
var resourceGroup:FlxTypedGroup<FlxSprite>;
var enemyGroup:FlxTypedGroup<FlxSprite>;
var maxItems:Int = 100;
var bar1:FlxBar;
var bartxt1:FlxText;
var bar2:FlxBar;
var bartxt2:FlxText;
var loopR:FlxAsyncLoop;
var loopE:FlxAsyncLoop;
public function addR()
{
var wood = new FlxSprite(FlxG.random.int(100, 2500), FlxG.random.int(100, 1800));
wood.makeGraphic(40, 100, FlxColor.BROWN);
resourceGroup.add(wood);
bar1.value = (resourceGroup.members.length / maxItems) * 100;
bartxt1.text = "Loading resources... " + resourceGroup.members.length + " / " + maxItems;
bartxt1.screenCenter();
FlxG.collide(player, resourceGroup) // should not need to call `seperate` - it's automatic when using `collide` vs `overlap`
}
public function addE()
{
var enemy = new FlxSprite(FlxG.random.int(300, 2500), FlxG.random.int(300, 1800));
enemy.makeGraphic(32, 32, FlxColor.RED);
enemyGroup.add(enemy);
FlxG.collide(enemyGroup, resourceGroup)
FlxG.collide(player, resourceGroup)
bar2.value = (enemyGroup.members.length / maxItems) * 100;
bartxt2.text = "Loading enemys... " + enemyGroup.members.length + " / " + maxItems;
bartxt2.screenCenter();
}
override public function create()
{
super.create();
resourceGroup = new FlxTypedGroup<FlxSprite>(maxItems);
enemyGroup = new FlxTypedGroup<FlxSprite>(maxItems);
loopR = new FlxAsyncLoop(maxItems, addR);
loopE = new FlxAsyncLoop(maxItems, addE);
FlxG.camera.zoom = 0.5;
playerPos = FlxPoint.get();
ground = new FlxSprite(0, 0);
ground.makeGraphic(2500, 1800, FlxColor.GREEN);
add(ground);
player = new FlxSprite(100, 100);
player.loadGraphic(AssetPaths.n1__png);
axe = new FlxSprite(player.x + 60, player.y);
axe.loadGraphic(AssetPaths.axeR__png);
camera = new FlxCamera(0, 0, 2500, 1800);
camera.bgColor = FlxColor.TRANSPARENT;
camera.setScrollBoundsRect(0, 0, ground.width, ground.height);
FlxG.cameras.reset(camera);
camera.target = player;
progressR = new FlxGroup();
bar1 = new FlxBar(0, 0, LEFT_TO_RIGHT, FlxG.width, 50, null, "", 0, 100, true);
bar1.value = 0;
bar1.screenCenter();
progressR.add(bar1);
bartxt1 = new FlxText(0, 0, FlxG.width, "Loading resources... 0 / " + maxItems);
bartxt1.setFormat(null, 28, FlxColor.WHITE, CENTER, OUTLINE);
bartxt1.screenCenter();
progressR.add(bartxt1);
progressE = new FlxGroup();
bar2 = new FlxBar(0, 0, LEFT_TO_RIGHT, FlxG.width, 50, null, "", 0, 100, true);
bar2.value = 0;
bar2.screenCenter();
progressE.add(bar2);
bartxt2 = new FlxText(0, 0, FlxG.width, "Loading enemys... 0 / " + maxItems);
bartxt2.setFormat(null, 28, FlxColor.WHITE, CENTER, OUTLINE);
bartxt2.screenCenter();
progressE.add(bartxt2);
resourceGroup.visible = false;
resourceGroup.active = false;
enemyGroup.visible = false;
enemyGroup.active = false;
add(progressR);
add(progressE);
add(resourceGroup);
add(enemyGroup);
add(loopR);
}
public function gamePlay(elapsed:Float):Void
{
if (FlxG.keys.pressed.LEFT && move)
{
player.x -= 5; // why +/- position and not .velocity?
// THIS is probably not right - you want to use a sprite sheet with animations and call axe.play("left") or something...
axe.loadGraphic(AssetPaths.axeL__png);
}
if (FlxG.keys.pressed.RIGHT && move)
{
player.x += 5;
axe.loadGraphic(AssetPaths.axeR__png);
}
if (FlxG.keys.pressed.UP && move)
{
player.y -= 5;
}
if (FlxG.keys.pressed.DOWN && move)
{
player.y += 5;
}
axe.x = player.x + 60;
axe.y = player.y;
playerPos = FlxPoint.get();
playerPos = player.getMidpoint();
for (e in enemyGroup)
{
FlxVelocity.moveTowardsPoint(e, playerPos, 70);
}
}
public function loading(elapsed:Float):Void
{
if (!loopR.started)
{
loopR.start();
}
else if (loopR.finished)
{
resourceGroup.visible = true;
progressR.visible = false;
resourceGroup.active = true;
progressR.active = false;
loopR.kill();
loopR.destroy();
add(loopE);
loopE.start();
}
else if (loopE.finished)
{
move = true;
add(player);
add(axe);
enemyGroup.visible = true;
progressE.visible = false;
enemyGroup.active = true;
progressE.active = false;
loopE.kill();
loopE.destroy();
}
}
override public function update(elapsed:Float)
{
super.update(elapsed);
if (!move)
{
loading(elapsed);
}
else
{
gamePlay(elapsed);
}
}
}

How can I use round dots in my animated line in HTML5 canvas?

I am using line creation on HTML5 canvas to create two animated dashed paths, one after the other. I simply want to change the dashes to rounded dots.
I've seen setting context lineCap ='round', etc but it doesn't work
var canvas;
var ctx;
var pathColor="black";
function Vector(x, y)
{
this.X = x;
this.Y = y;
}
var m1, c1;
var pCount1=0;
var pCount2=0;
var pathInt = null;
var points1 = [new Vector(1400, 1500),new Vector(1365, 1495),new Vector(1330, 1490),new Vector(1295, 1485),new Vector(1260, 1480)];
var points2 = [new Vector(1280, 480),new Vector(1195, 470),new Vector(1110, 460),new Vector(1080, 450),new Vector(1000, 240),new Vector(1300, 140),new Vector(1580, 140),new Vector(1590, 180),];
function getClr() {
if(pathColor=="lightgrey") {
pathColor="black";
}
else {
pathColor="lightgrey";
}
}
window.onload = function()
{
m1 = document.querySelector("#b");
c1 = m1.getContext("2d");
pathInt = setInterval(function(){ pathTo(); }, 250);
};
function pathTo()
{
if(pCount1 <= points1.length && points1.length!=0)
{
c1.strokeStyle = pathColor;
c1.lineWidth = 15;
c1.setLineDash([25, 10]);
c1.beginPath();
c1.moveTo(points1[0].X, points1[0].Y);
for(var i = 0; i < pCount1; i++)
{
c1.lineTo(points1[i].X, points1[i].Y);
}
pCount1++;
c1.stroke();
}
else if(pCount2 <= points2.length && points2.length!=0)
{
c1.strokeStyle = pathColor;
c1.lineWidth = 15;
c1.setLineDash([25, 10]);
c1.beginPath();
c1.moveTo(points2[0].X, points2[0].Y);
for(var i = 1; i < pCount2; i++)
{
c1.lineTo(points2[i].X, points2[i].Y);
}
pCount2++;
c1.stroke();
pCount2++;
}
else {
clearInterval(pathInt);
setTimeout(function(){ redoPath(); }, 5000);
}
}
function redoPath() {
pCount1=0;
pCount2=0;
//c1.clearRect(0, 0, m1.width, m1.height);
getClr();
pathInt = setInterval(function(){ pathTo(); }, 250);
}
<canvas id="b" width="1746" height="1746" style="position:absolute;top:100px;left:50px;z-index:100;">
</canvas>
I know this must be possible, ..but not sure which properties I need to change. Any help is appreciated.

dynamically change an image via c# script in Xamarin forms

I have a cross platform app which I need to show a set of images when the user clic in a button, so, I put these image files named as "img000.png" to "img029.png" in a folder in the PCL solution and make al these images as "EmbeddedResource", after,I fill a List with all these images and its work fine at now, i.e. the image is shown like I want, but when I click in the button to show the next image in the list the image don't go to the next.
I have this:
//...
public class ImageSetPage : BasePage
// BasePage encapsule a ContentPage...
{
private string directory = "MyApp.Assets.";
private int idx = 0;
protected StackLayout _mainLayout;
protected StackLayout _buttonStack;
protected Image _btnPrevI;
protected Label _displayName;
protected Image _btnNestI;
protected Image _image;
protected List<Image> _Images;
public ImageSetPage()
{
this._Images = getImages();
var prevI_Tap = new TapGestureRecognizer();
prevI_Tap.Tapped += (s, e) =>
{
onClick("pvi");
};
var nextI_Tap = new TapGestureRecognizer();
nextI_Tap.Tapped += (s, e) =>
{
onClick("nti");
};
/// begin layout
base.Title = "set of Images";
this._mainLayout = new StackLayout()
{
Orientation = StackOrientation.Vertical,
BackgroundColor = Color.Black,
Padding = new Thickness(0, 10, 0, 0)
};
this._btnPrevI = new Image()
{
Aspect = Aspect.AspectFill,
Source = ImageSource.FromResource(directory+"prevbtn.png")
};
_btnPrevI.GestureRecognizers.Add(prevI_Tap);
this._displayName = new Label()
{
Style = Device.Styles.SubtitleStyle,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
TextColor = Color.White,
};
this._btnNextI = new Image()
{
Aspect = Aspect.AspectFill,
Source = ImageSource.FromResource(directory + "nextbtn.png")
};
_btnNextI.GestureRecognizers.Add(nextI_Tap);
this._buttonStack.Children.Add(_btnPrevI);
this._buttonStack.Children.Add(_displayName);
this._buttonStack.Children.Add(_btnNextI);
this._mainLayout.Children.Add(_buttonStack);
FillImage(idx);
this._mainLayout.Children.Add(this._image);
this.Content = this._mainLayout;
}
private void FillImage(int i)
{
this._displayName.Text = "Image n# " + FillWithZeroes(i);
// [EDIT 1] cemented these lines ...
// this._image = null;
// mi = _images[i];
// this._image = mi;
// [EDIT 2] the new try
string f = directory + "imgs.img0" + FillWithZeroes(i) + ".png";
this._image = new Image() {
VerticalOptions = LayoutOptions.Center,
Soruce = ImageSource.FromResource(f)
};
// In this way the image show and don't change when click
// this Fking* code is a big S_OF_THE_B*
// The Xamarin and the C# is brothers of this FKing* code
}
private void onClick(string v)
{
string vw = v;
if (vw.Equals("pvi")) idx--;
if (vw.Equals("nti")) idx++;
if (idx <= 0) idx = 29;
if (idx >= 29) idx = 0;
FillImage(idx);
vw = "";
}
private string FillWithZeroes(int v)
{
string s = v.ToString();
string r = "";
if (s.Length == 1) { r = "0" + s; } else { r = s; }
return r;
}
// to fill a list of Images with files in a PCL folder
private List<Image> getImages()
{
string directory = "MyApp.Assets.";
List<Image> imgCards = new List<Image>();
int c = 0;
for (c = 0; c < 30;c++) {
string f = directory + "imgs.img0" + FillWithZeroes(c) + ".png";
Image img = new Image();
img.Source = ImageSource.FromResource(f);
imgCards.Add(img);
}
return imgCards;
}
// ...
}
but the image don't change, i.e. change, like I see in debug, but don't show in the Layout when I click in the buttons.
Maybe I'm doing it wrong.
Can someone here help me?
thanks in advance

HaxeFlixel animations not playing

I'm trying to make a simple little game in HaxeFlixel, where you play as a ghost and you go around an apartment complex knocking on doors. There's a bit more to it than that, but that's the fundamental idea. Anyway, I've currently got the ghost moving from door to door, and knocking on them, but for some reason the animation where the tenant opens the door isn't triggering.
Here is the state:
package;
import flixel.addons.display.FlxBackdrop;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.FlxG;
import flixel.group.FlxTypedGroup;
import flixel.group.FlxTypedGroupIterator;
import flixel.text.FlxText;
import flixel.util.FlxPoint;
/**
* ...
* #author ...
*/
class GhostState extends FlxState
{
var ghost:FlxSprite;
var hall:FlxBackdrop;
var wall:FlxSprite;
var knock :Array<FlxText>;
public var doors:FlxTypedGroup<Door>;
public var speed = 0;
public var inTransit:Bool;
public var knockCount = 0;
public var doneWithThisDoor = false;
public var doorIndex = 0;
public function justPressed():Bool
{
#if mobile
var returnVal = false;
for (touch in FlxG.touches.list)
{
returnVal = touch.justPressed;
}
return returnVal;
#else
return FlxG.mouse.justPressed;
#end
}
public function pressed():Bool
{
#if mobile
var returnVal = false;
for (touch in FlxG.touches.list)
{
returnVal = touch.pressed;
}
return returnVal;
#else
return FlxG.mouse.pressed;
#end
}
public function justReleased():Bool
{
#if mobile
var returnVal = false;
for (touch in FlxG.touches.list)
{
returnVal = touch.justReleased;
}
return returnVal;
#else
return FlxG.mouse.justReleased;
#end
}
public function clickCoords():FlxPoint
{
#if mobile
var returnVal = new FlxPoint();
var i=0;
for (touch in FlxG.touches.list)
{
i++;
returnVal.x += touch.screenX;
returnVal.y += touch.screenY;
}
returnVal.x /= i;
returnVal.y /= i;
return returnVal;
#else
return new FlxPoint(FlxG.mouse.screenX, FlxG.mouse.screenY);
#end
}
override public function create()
{
hall = new FlxBackdrop("assets/images/Stage3/hall wall.png", 0, 0, true, false);
add(hall);
doors = new FlxTypedGroup<Door> ();
add(doors);
#if web
doors.add(new Door(((FlxG.width/2) - 259) + 465 + 175, (FlxG.height - 280) / 2, this, "assets/images/Stage3/door.png"));
#else
doors.add(new Door(((FlxG.width - 250-(175/2)-10) / 2) + 465 + 175, (FlxG.height - 280) / 2, this, "assets/images/Stage3/door.png"));
#end
ghost = new FlxSprite(FlxG.width / 2, FlxG.height / 2, "assets/images/Stage3/chicken ghost.png");
ghost.loadGraphic("assets/images/Stage3/chicken ghost.png", true, 75, 100);
ghost.animation.add("right", [0], 30, true);
ghost.animation.add("forward", [1], 30, true);
ghost.animation.add("back", [2], 30, true);
add(ghost);
speed = 0;
super.create();
knock = new Array();
knock.push(new FlxText(ghost.x+25, ghost.y-35, -1, "*knock*", 20));
knock.push(new FlxText(ghost.x+25, ghost.y - 85, -1, "*knock*", 20));
for (member in knock)
{
member.color = 0x000000;
add(member);
member.kill();
}
nextDoor();
}
public function nextDoor()
{
inTransit = true;
if (ghost.x <= doors.members[doorIndex].x)
{
speed = 10;
ghost.animation.play("right");
}
else
{
speed = 0;
inTransit = false;
doorIndex++;
ghost.animation.play("forward");
}
}
override public function update()
{
hall.x -= speed;
/* var i = 0;
while (i < doors.members.length)
{
var basic = doors.members[i++];
if (basic != null && basic.exists && basic.active)
{
basic.update();
}
}*/
if (inTransit)
{
nextDoor();
}
super.update();
if (justPressed()&&!inTransit)
{
if (knockCount == 2)
{
knockCount = 0;
for (member in knock)
{
member.kill();
}
doors.members[doorIndex-1].open();
FlxG.watch.add(this,"doorIndex");
nextDoor();
}
else
{
knock[knockCount].revive();
knockCount++;
}
}
}
}
And here is the Door class:
package;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.FlxG;
/**
* ...
* #author ...
*/
class Door extends FlxSprite
{
var state:GhostState;
var firstPass = true;
public function new(X:Float=0, Y:Float=0, level:GhostState, ?SimpleGraphic:Dynamic)
{
super(X, Y, SimpleGraphic);
loadGraphic("assets/images/Stage3/door.png", true, 175, 250, false);
animation.add("open", [0, 1, 2, 3, 4, 5], 30, false);
animation.add("close", [5,4,3,2,1,0], 30, false);
state = level;
//this.animation.play("open");
//state.doors.add(this);
}
public override function update():Void
{
if (firstPass)
{
if (isOnScreen())
{
state.doors.add(new Door(x + 465 + 175, (FlxG.height - 280) / 2, state));
//state.add(new Door(x+465+175, (FlxG.height - 280) / 2, state));
firstPass = false;
}
}
this.x -= state.speed;
if (this.x <= 0-this.width)
{
this.destroy();
}
}
public function open()
{
trace("open");
animation.play("open", true, 0);
}
public function close()
{
animation.play("close", true, 0);
}
}
Some information abut the code, as I'm bad at remembering to comment:
doors is a group that contains all doors in the state
doorIndex is the next door the ghost is supposed to move to next (so
doorIndex - 1 is the door it's at right now)
Why isn't it triggering? And how should I go about fixing this?
The update(elapsed:Float) function has an elapsed paremeter and inside of the function you override you must call the super.update(elapsed) parent function becase the animations are computed in this parent function.

Resources