Forum

> > CS2D > Scripts > Move an image away from a player
Forums overviewCS2D overview Scripts overviewLog in to reply

English Move an image away from a player

6 replies
To the start Previous 1 Next To the start

old Move an image away from a player

Mami Tomoe
User Off Offline

Quote
1
2
3
4
5
6
7
8
local x, y, rot = player(id,"x"), player(id,"y"), player(id,"rot")
local Arrow = image('gfx/terraria/equipment/special/Arrow.png', 3, 0, 1)
imagepos(Arrow, x, y, rot)
x = PLAYERS[id].MOUSEX
y = PLAYERS[id].MOUSEY
tween_move(Arrow, 10000, x, y, rot)
tween_alpha(Arrow, 5000, 0)
timer(5000, 'freeimage', Arrow)

Currently this is a code I use inside an attack hook.
• The code is supposed to:
√ Show an image at the player's position.
× Move the image away like a bullet would.
√ Slowly fade away the image.

But the tween_move doesn't move the image to the right position (currently set on a random variable so ignore the
x =
and
y =
below
imagepos
)

Does anyone know what x and y variables I'm supposed to use?

old Re: Move an image away from a player

-3Jlou_nTu4-
User Off Offline

Quote
You need use
1
2
player(id_of_player, "mousemapx")
player(id_of_player, "mousemapy")

Because, if you try use that:
1
2
player(id_of_player, "mousex")
player(id_of_player, "mousey")
then you will get the offset in pixels relative to the upper left corner of the client area of the game window, and not the coordinates of the map, as in the first case

old Re: Move an image away from a player

-3Jlou_nTu4-
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local IM_EFFECT_MSECS = 3000	--how long image will be move in milliseconds
local IM_EFFECT_OFFSETX = 0	--position to which the image will be moved from "center" position
local IM_EFFECT_OFFSETY = -30
local IM_EFFECT_CENTERX = 0	--"center" position, (0, 0) - position of player
local IM_EFFECT_CENTERY = 0

local im = {}

function effectIm(param)
	param = tonumber(param)
	local pImEl = im[param]

	local time = IM_EFFECT_MSECS / 10
	local distX = IM_EFFECT_OFFSETX/time * (time-pImEl.timer)
	local distY = IM_EFFECT_OFFSETY/time * (time-pImEl.timer)

	if pImEl then
		imagepos(pImEl.id,
			player(param, "x") + IM_EFFECT_CENTERX + distX,
			player(param, "y") + IM_EFFECT_CENTERY + distY,
			0
		)
		tween_move(
			pImEl.id,
			IM_EFFECT_MSECS,
			player(param, "x")+IM_EFFECT_OFFSETX,
			player(param, "y")+IM_EFFECT_OFFSETY
		)
		pImEl.timer = pImEl.timer - 1
		if pImEl.timer==0 then
			freeimage(pImEl.id)
			pImEl.id = nil
		end
	end
end

function fillIm(plid)
	if plid and im[plid] then
		if im[plid].id then
			freetimer("effectIm", tostring(plid))
			freeimage(im[plid].id)
			im[plid].id = nil
		end
		im[plid] = {
			id = image("gfx/gametitle.png", 0, 0, 3),
			timer = IM_EFFECT_MSECS / 10,
		}
	end
end

function startImEffect(plid)
	if plid>=#im then
		table.insert(im, {id = nil, timer = 0})
	end

	fillIm(plid)
	timer(10, "effectIm", tostring(plid), IM_EFFECT_MSECS / 10)
end

old Re: Move an image away from a player

Mami Tomoe
User Off Offline

Quote
What I mean't is that the image is supposed to move like a bullet would, that means it doesn't stick to player it leaves the player and moves to the direction the player is looking at.

Imagine the image is a bullet that came out of a gun that's how the image is supposed to move.

old Re: Move an image away from a player

-3Jlou_nTu4-
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
addhook("attack", "attack_proc")

local IM_MAXDIST = 8	--the maximum distance(in tiles) the image will go through and then disappear
local IM_PATH = "gfx/cursor.bmp"

function imMoveLikeBullet_free(param)
	local imId = tonumber(param)
	if imId then
		freeimage(imId)
	end
end

function imMoveLikeBullet(plid)
	if plid and player(plid, "exists") and player(plid, "health")>0 then
		local imId = image(IM_PATH, 0, 0, 3)
		local rot = player(plid, "rot")
		local targX = player(plid, "x") + (32 * IM_MAXDIST) * math.sin(math.rad(rot))
		local targY = player(plid, "y") - (32 * IM_MAXDIST) * math.cos(math.rad(rot))
		imagepos(imId, player(plid, "x"), player(plid, "y"), rot)
		tween_move(imId, 100, targX, targY)
		timer(100, "imMoveLikeBullet_free", tostring(imId), 1)
	end
end

function attack_proc(plid)
	imMoveLikeBullet(plid)
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview