This is my code for the enemies in my game
//Collision
if (place_meeting(x,y,Object_Wall))
{
speed = 0
direction = point_direction(x,y,Object_Wall.x,Object_Wall.y) + random_range(160,200)
speed = sp / 2
time = random_range(room_speed * 0.75,room_speed * 3)
}
When the zombie hits a wall, it should turn back and walk the other way.
This works most of the time but sometimes they will just drift through walls and if they are following the player and he goes next to a wall they go through it.
I don't know why it doesn't work sometimes and would like help to fix this.
I am using Object_Wallas a parent object and they work with it but the problem occurs with it's children.
When you use point_direction(x,y,Object_Wall.x,Object_Wall.y), it's not object which was found using place_meeting(x,y,Object_Wall).
place_meeting(x,y,Object_Wall) will check collsion with all instances of object Object_Wall. But when you use in point_direction object id instead instance id, GM will take first (in creation order) instance of object Object_Wall.
You can take instance id using, for example, collision_circle function.
But I recommend use Collision event and other keyword inside it.
Related
I want to play a little Animated Sprite once, I mean only when the scene is visited the first time. I am trying to connect some signals in those ways:
on_scene_ready():
Animated_sprite.play("animation_name")
on_Animated_sprite_animation_finished():
Animated_sprite.hide()
And that's working correctly. But it repeats every time the scene is entered.
I tried a solution found on another forum that seemed to me similar to my issue: put a global variable (but doesn't work and I bet I make it bad)
var has_not_played = true
on_scene_ready():
if has_not_played:
Animated_sprite.play(animation_name)
on_Animated_sprite_animation_finished():
Animated_sprite.hide()
has_not_played = false
Any suggestions?
Thanks.
As long as you create a new instance of the scene (what you most likely do, as we can see) your has_not_played variable will always be instanced as true as well. Setting it to false will not help here then.
One simple solution would be to create a autoload script to hold all level informations you need your programm to save, after the scene was exited.
These are infos you most likely want to save as well, when your game is saved.
As an example you could do something like this.
Create a script LevelInfos.gd. Mine looked like this:
extends Node
var level_infos : Dictionary = {
"level_1" : {
"start_animation_played" : false
}
}
Then add this to Project -> project settings -> Autoload with the NodeName LevelInfos.
Now you can reuse your existing code like this:
on_scene_ready():
if not LevelInfos.level_infos["level_1"]["start_animation_played"]:
Animated_sprite.play(animation_name)
on_Animated_sprite_animation_finished():
Animated_sprite.hide()
LevelInfos.level_infos["level_1"]["has_already_loaded"] = true
This should make it so it only gets played the first time the scene is visited, after you start the game.
Code wise I guess it would be better to make dedicated variables instead of using a dictionary, but it works as a example and could be easily saved as an JSON, if needed.
I am building a Ruby Sinatra app with Datamapper as the ORM. I have come across a point that I can't find a decent solution and thought to hit StackOverflow for a solution.
I am needing to compare the old value of a filed with the new value entered by the user to do a small computation.
For example
car = Listing.all(type: :car).first
car.price # 200
car.price = 100
car.save # ~> discount = ( 200 - 100 / 200 ) * 100 ~= 50% - alert watchers
How can I find the old value within the modal?
I have an idea to abstract all modals with custom classes. This will make such operation possible. But wonder if it will add complexity unnecessarily.
Thanks in advance.
You can pull out the old value via model.original_attributes. Something like this should work for your case:
car = Listing.all(type: :car).first
car.price = 100
old_price = car.original_attributes[Listing.properties[:price]] # => 200
You should be able to put that in a before save hook and run your calculations and notifications from there.
HTH :)
(BTW I tested this with DM 1.2.1. I imagine that is the version you are using as well since the DM project is kind of dead-ish these days, but just so you should be aware)
The main running example of the Angular Dart tutorial is a Recipe Book app. The exercise at the end of the Chapter 5 on filters and services suggests trying to "create a [custom] filter that will multiply all the amounts [of each ingredient listed] in the recipes" thus allowing a "user to double, triple, or quadruple the recipe." E.g. an ingredient of "1/2 cup of flour" would become "1 cup of flour" when doubled.
I have written such a custom filter: it takes a list of Ingredients (consisting of a quantity and a description) and returns a new list of new Ingredients (with increased quantities), but I am getting the following error:
5 $digest() iterations reached. Aborting!
My question is: what is the required and/or permitted behavior of an AngularDart custom filter call() method? E.g., clearly it is permitted to remove (i.e. filter) elements from its input list, but can it also add new or replace elements? The Dart angular.core NgFilter documentation simply says that a "filter is a class with a call method". I have not found more details.
Extrapolating from the answer to this AngularJS post, it would seem that repeated invocations of call() should (eventually?) yield "the same result". If so, this would be a reasonable constraint.
Yielding "the same result" could mean that call() needs to be idempotent, but in the case of Dart such idempotence should be relative to == (object equivalence) not identical() (object identity), IMHO. I ran a few tests using the following small example to illustrate the issues:
main.dart
import 'package:angular/angular.dart';
class A { }
#NgFilter(name:'myFilter') class MutatingCustomFilter {
final A _a = new A();
call(List list) => new List.from(list)..add(_a); // runs ok.
// call(List list) => new List.from(list)..add(new A()); // gives error
}
class MyAppModule extends Module {
MyAppModule() { type(MutatingCustomFilter); }
}
main() => ngBootstrap(module: new MyAppModule());
index.html excerpt
<ul>
<li ng-repeat="x in [1,2,3] | myFilter">{{x}}</li>
</ul>
If I change the body of class A to be
#override bool operator==(other) => true;
#override int get hashCode => 1;
which makes all instances of A considered ==, then the second implementation of call() in main.dart (the one with add(new A())) still gives an error (though a different one).
I can see how to solve the tutorial exercise without use of a custom filter, but I am trying to not give up on the challenge of finding a filter that will work as requested. I am new to Angular and decided to jump in with AngularDart, so any help in explaining the effects of the various flavors of call(), or in finding documentation for the expected behavior of call(), (or letting me know if you think such a custom filter simply cannot be written!) would be appreciated.
Too many iterations
When angular detects a change in the model, it executes a reaction function. The reaction function can further change the model. This would leave the model in inconsistent state. For this reason we re-run the change detection, which can further create more changes. For this reason we keep re-running the changes until the model stabilizes. But how many times should we rerun the change detection before giving up? By default it is 5 times. If the model does not stabilize after 5 iteration we give up. This is what is going on in your case.
Change Detection
When has object changed? one can use identical or == (equals). Good arguments can be made for each, but we have chosen to use identical because it is fast and consistent. Using == (equals) is tricky and it would negatively impact the change detection algorithm.
Filters and arrays
When a filter which operates an an array, executes it has no choice but to create a new instance of the array. This breaks identical, but luckily it is fed into ng-repeat which uses its own algorithm for array contents detection rather the array detection. While the array does not have to be identical between runs, its content must be. Otherwise ng-repeat can not tell the difference between insertions and changes, which it needs to do proper animations.
Your code
The issue with your filter is that it creates new instance on each iteration of the digest loop. These new instances prevent the model from stabilizing and hence the error. (There are plans to solve this issue, but it will be few weeks before we get there.)
Solution
Your solutions is attempting to create a filter which consumes the whole array and then attempts to create a new array, for the ng-repeat. A different (prefered) solution would be to leave the ng-repeat iteration as is, and instead place the filter on the binding which is creating the qty and apply it there.
<span>{{recipe.qty | myFilter:multiply}}</span>
I wonder how could I get the correct palm rotation angle (along Y-axis) from Leap Motion (I'm using the latest LeapJS API)?
I have tried both rotationAngle(sinceFrame) method and the frame._rotation. However, the rotationAngle() returns a very small value close to 0, which seems to be the rotation angle calculated based on the current frame (how to define the sinceFrame in Leap.loop(function(frame)){}, as it seems only records data from current frame).
And the frame._rotation returns a 3*3 matrix (which I've no idea about what is it, as it's not included in the API documentation). Therefore, I wonder if there's any way to get the correct rotation angle of Y-axis from these methods?
The latest version of the leap.js library includes a roll() function as a member of the Hand object. (It also has pitch() and yaw(), if roll() isn't all you want.) The roll() function is defined as:
Hand.prototype.roll = function() {
return Math.atan2(this.palmNormal[0], -this.palmNormal[1]);
}
As an aside, since you don't need to use rotationAngle() to get the current palm angles, the rotationAngle() function gives you the rotation that has occurred between two frames. For sinceFrame, you can either store a reference to the starting frame or get a past frame from the history buffer maintained by the Controller object. Although the Leap.loop() function doesn't give you access to its Controller object directly, you should be able to create your own. Note that frame._rotation is an internal variable that is used in the calculation of the transformations between two frames. It isn't much use on its own.
If I have multiple GeoCoordinateWatchers in a WP7 application, they seem to cause conflict with one another. I would assume that if I have a watcher setup like:
new GeoCoordinateWatcher(GeoPositionAccuracy.High) { MovementThreshold = 0 }
and another setup like:
That the value from the first should be extremely accurate whereas the second one should be used as a point of reference.
new GeoCoordinateWatcher(GeoPositionAccuracy.Default) { MovementThreshold = 1000 };
However, the second one causes the first coordinates to jump all over the place. If I comment out the second, the first works as expected. Any idea why?
Try setting the same accuracy and see if you get the same values.